【HDOJ】1695 GCD

莫比乌斯反演简单题目。

  1 /* 1695 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 using namespace std;
 20 //#pragma comment(linker,"/STACK:102400000,1024000")
 21 
 22 #define mpii            map<int,int>
 23 #define vi                vector<int>
 24 #define pii                pair<int,int>
 25 #define rep(i, a, n)     for (int i=a;i<n;++i)
 26 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 27 #define pb                 push_back
 28 #define mp                 make_pair
 29 #define all(x)             (x).begin(),(x).end()
 30 #define SZ(x)             ((int)(x).size())
 31 #define lson            l, mid, rt<<1
 32 #define rson            mid+1, r, rt<<1|1
 33 
 34 const int maxn = 1000005;
 35 int P[maxn];
 36 bool visit[maxn];
 37 int mu[maxn], m = 0;
 38 
 39 void init() {
 40     int i, j, k;
 41     
 42     memset(visit, false, sizeof(visit));
 43     mu[1] = 1;
 44     for (i=2; i<maxn; ++i) {
 45         if (!visit[i]) {
 46             P[m++] = i;
 47             mu[i] = -1;
 48         }
 49         for (j=0; j<m; ++j) {
 50             k = P[j] * i;
 51             if (k > maxn)
 52                 break;
 53             visit[k] = true;
 54             if (i%P[j] == 0) {
 55                 mu[k] = 0;
 56                 break;
 57             } else {
 58                 mu[k] = -mu[i];
 59             }
 60         }
 61     }
 62 }
 63 
 64 int main() {
 65     
 66     #ifndef ONLINE_JUDGE
 67         freopen("data.in", "r", stdin);
 68         freopen("data.out", "w", stdout);
 69     #endif
 70     
 71     int a, b, c, d, k;
 72     int tt;
 73     __int64 ans, tmp;
 74     
 75     init();
 76     scanf("%d", &tt);
 77     rep(t, 1, tt+1) {
 78         scanf("%d %d %d %d %d",&a,&b,&c,&d,&k);
 79         if (k == 0) {
 80             ans = 0;
 81         } else {
 82             b /= k;
 83             d /= k;
 84             if (b > d)
 85                 swap(b, d);
 86             ans = tmp = 0;
 87             rep(i, 1, b+1)
 88                 ans += 1LL * mu[i] * (b/i) * (d/i);
 89             rep(i, 1, b+1)
 90                 tmp += 1LL * mu[i] * (b/i) * (b/i);
 91             #ifndef ONLINE_JUDGE
 92                 printf("%I64d %I64d
", ans, tmp);
 93             #endif
 94             ans -= tmp/2;
 95         }
 96         printf("Case %d: %I64d
", t, ans);
 97     }
 98     
 99     #ifndef ONLINE_JUDGE
100         printf("time = %d
", (int)(clock()));
101     #endif
102     
103     return 0;
104 }
原文地址:https://www.cnblogs.com/bombe1013/p/4609272.html