BZOJ2301: [HAOI2011]Problem b

莫比乌斯反演

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<vector>
 6 #include<cmath>
 7 #include<queue>
 8 #define MAXN 50000+10
 9 #define INF 0x7f7f7f7f
10 #define LINF 0x7f7f7f7f7f7f7f7f
11 #define ll long long
12 #define pb push_back
13 #define ft first
14 #define sc second
15 #define mp make_pair
16 #define pil pair<int,ll>
17 #define pll pair<ll,ll>
18 using namespace std;
19 int isp[MAXN];
20 int mu[MAXN],s[MAXN];
21 vector<int> p;
22 void getmu(){
23     s[1]=mu[1]=1;
24     for(int i=2;i<MAXN;i++){
25         if(!isp[i]){
26             p.pb(i);
27             mu[i]=-1;
28         }    
29         for(int j=0;j<p.size()&&i*p[j]<MAXN;j++){
30             isp[i*p[j]]=1;
31             if(i%p[j]==0){
32                 mu[i*p[j]]=0;
33                 break;
34             }
35             mu[i*p[j]]=-mu[i];
36         }
37         s[i]=s[i-1]+mu[i];
38     }
39 }
40 int calc(int n,int m,int k){
41     if(n>m)swap(n,m);
42     n/=k,m/=k;
43     int ret=0,lst;
44     for(int i=1;i<=n;i=lst+1){
45         lst=min(n/(n/i),m/(m/i));
46         ret+=((s[lst]-s[i-1])*(n/lst)*(m/lst));
47     }
48     return ret;
49 }
50 int a,b,c,d,k;
51 int main()
52 {
53     //freopen("data.in","r",stdin);
54     getmu();
55     int T;
56     scanf("%d",&T);
57     while(T--){
58         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);a--,c--;
59         int ans=calc(b,d,k)-calc(a,d,k)-calc(b,c,k)+calc(a,c,k);
60         printf("%d\n",ans);
61     }
62     return 0;
63 }

 

posted @ 2018-01-27 23:56  white_hat_hacker  阅读(...)  评论(...编辑  收藏