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;
    }
    
    
  • 相关阅读:
    51nod1347(简单逻辑)
    决策树和随机森林
    朴素贝叶斯
    k近邻算法(KNN)
    模型训练与优化
    Navicat 提示 Access violation at address ***(如004ECCF4) in module ‘navicat.exe’. Read of address ***(如00000048)
    cookie and session
    .gitignore设置
    前端资料
    CentOS添加环境变量
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10914895.html
Copyright © 2011-2022 走看看