BZOJ2301【莫比乌斯函数】

/* I will wait for you */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int maxn=50010;
const int maxm=1000000;
const int maxs=100;
const int INF=1<<29;
const int P=1000000007;
const double error=1e-9;

int a,b,c,d,p,mu[maxn],f[maxn],vis[maxn],pr[maxn];

void init()
{
pr[1]=mu[1]=1;
for(int i=2;i<maxn;i++) if(!vis[i])
for(int j=2*i;j<maxn;j+=i) vis[j]=1,pr[j]=i;

for(int i=2;i<maxn;i++)
mu[i]=vis[i]?(i/pr[i]%pr[i]?-mu[i/pr[i]]:0):-1;

for(int i=2;i<maxn;i++) mu[i]+=mu[i-1];
}

int work(int n,int m)
{
int ans=0,pos;if(n>m) swap(n,m);
for(int i=1;i<=n;i=pos+1)
{
pos=min(n/(n/i),m/(m/i));
ans+=(mu[pos]-mu[i-1])*(n/i)*(m/i);
}
return ans;
}

int main()
{
init();int t;scanf("%d",&t);

while(t--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&p);
(a-=1)/=p;b/=p;(c-=1)/=p;d/=p;
printf("%d\n",work(b,d)+work(a,c)-work(a,d)-work(b,c));
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客