Coprime Integers Gym - 101982B

https://cn.vjudge.net/problem/Gym-101982B 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int N=1e7+9;
const int M=4e5+9;

bool check[N+10];
int prime[N+10];
int mu[N+10];
ll sum[N+10];
ll a,b,c,d;
void Moblus()
{
    mem(check,0);
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=N;i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;jN) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }

    for(int i=1;i<=N;i++) sum[i]=sum[i-1]+mu[i];
}

ll cal(int n ,int m)
{
    ll ans=0;
    if(n>m) swap(n,m);
    for(int i=1,r;i<=n;i=r+1)
    {
        r=min(n/(n/i),m/(m/i));
        ans+=(sum[r]-sum[i-1])*(n/i)*(m/i);
    }
    return ans;
}

int main()
{
    //FAST_IO;
    //freopen("input.txt","r",stdin);

    cin>>a>>b>>c>>d;
    Moblus();
    ll ans=cal(b,d)-cal(a-1,d)-cal(c-1,b)+cal(a-1,c-1);
    cout<

 

你可能感兴趣的:(莫比乌斯反演)