hdu 1695 GCD(容斥)

题目链接

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 inline int read()
 5 {
 6     int x=0,f=1;char ch=getchar();
 7     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 8     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 9     return x*f;
10 }
11 
12 /********************************************************************/
13 
14 const int maxn = 1e5+7;
15 vector<int> pri[maxn];
16 
17 //分解成的质数
18 void init(){
19     for(int i = 2;i < maxn; i++){
20         int now = i;
21         for(int j = 2;j*j <= now;j++){
22             if(now%j == 0){
23                 pri[i].push_back(j);
24                 while(now%j == 0) now /= j;
25             }
26             if(now == 1) break;
27         }
28         if(now > 1)
29             pri[i].push_back(now);
30     }
31 }
32 
33 int solve(int x, int pos){
34     int res = 0;
35     for(int i = 1;i < (1 << pri[x].size());i++){
36         int num = 0;
37         int tmp = 1;
38         for(int j = 0;j < pri[x].size();j++){
39             if((i >> j)& 1){
40                 num++;
41                 tmp *= pri[x][j];
42             }
43         }
44         if(num & 1) res += pos/tmp;
45         else res -= pos/tmp;
46     }
47     return pos - res;
48 }
49 
50 int main(){
51     init();
52     int t;
53     t = read();
54     for(int i = 1;i <= t;i++){
55         int a, b, c, d, k;
56         //a = 1, d = 1;
57         a = read(); b = read(); c = read(); d = read(); k = read();
58         if(k == 0){
59             printf("Case %d: 0\n", i);
60             continue;
61         }
62         b /= k, d /= k;
63         if(b < d) swap(b, d);
64         ll ans = 0;
65         for(int j = 1;j <= b;j++){
66             ans += solve(j, min(j, d));
67         }
68         printf("Case %d: %lld\n", i, ans);
69     }
70     return 0;
71 }

 

posted @ 2018-09-12 19:55  ouyang_wsgwz  阅读(106)  评论(0编辑  收藏  举报