zoukankan      html  css  js  c++  java
  • 莫比乌斯反演模板--Gym 101982B

    莫比乌斯反演模板--Gym 101982B

    题意

    给你a,b,c,d四个数,求[a,b]与[c,d],互质的数的对数

    代码

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <sstream>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <iomanip>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int N = 1e7 + 100;
    const int MOD = 1e9 + 9;
    
    #define lson l, m, rt << 14
    #define rson m + 1, r, rt << 1 | 1
    #define F(i, l, r) for(int i = l;i <= (r);++i)
    #define RF(i, l, r) for(int i = l;i >= (r);--i)
    
    int prime[N], vis[N], cnt, mu[N], sum[N];
    void Isprime(int n)
    {
        mu[1] = 1;
        for(int i = 2;i <= n;++i)
        {
            if(!vis[i]) {prime[cnt++] = i;mu[i] = -1;}
            for(int j = 0;j < cnt && i * prime[j] <= n;++j)
            {
                vis[i * prime[j]] = 1;
                if(i % prime[j] == 0)
                    break;
                mu[i * prime[j]] = -mu[i];
            }
        }
        for(int i = 1;i <= n;++i)
            sum[i] = sum[i - 1] + mu[i];
    }
    
    LL a, b, c, d;
    LL solve(LL n, LL m)
    {
        if(n > m) swap(n, m);
        LL ans = 0;
        for(int i = 1, last;i <= n;i = last + 1)
        {
            last = min(n / (n / i), m / (m / i));
            ans += (sum[last] - sum[i - 1]) * (n / i) * (m / i);
        }
        return ans;
    }
    
    int main()
    {
        Isprime(1e7+5);
        cin >> a >> b >> c >> d;
        cout << solve(b, d) - solve(a - 1, d) - solve(b, c - 1) + solve(a - 1, c - 1);
        return 0;
    }
    
    
  • 相关阅读:
    codevs 3657 括号序列
    洛谷P1962 斐波那契数列
    Black Rock shooter
    codevs 2596 售货员的难题
    51Nod-1154 回文串划分
    UVA
    POJ3321[苹果树] 树状数组/线段树 + dfs序
    Hdu 4578 Transformation (线段树 分类分析)
    786B
    438D
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10914895.html
Copyright © 2011-2022 走看看