码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ2820 YY的GCD

时间:2015-02-16 16:51:06      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

妈呀这个世界的数论实在是太可怕了。。。

Orz iwtwiioi(给完传送门赶紧跑)

 

技术分享
 1 /**************************************************************
 2     Problem: 2820
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:3508 ms
 7     Memory:127760 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12  
13 using namespace std;
14 typedef long long ll;
15 const int N = 1e7 + 5;
16  
17 int n, m;
18 int p[N], u[N], g[N], cnt;
19 bool vis[N];
20  
21 inline int read() {
22   int x = 0;
23   char ch = getchar();
24   while (ch < 0 || 9 < ch)
25     ch = getchar();
26   while (0 <= ch && ch <= 9) {
27     x = x * 10 + ch - 0;
28     ch = getchar();
29   }
30   return x;
31 }
32  
33 void get_g() {
34   int i, j, k;
35   u[1] = 1;
36   for (i = 2; i < N; ++i) {
37     if (!vis[i])
38       p[++cnt] = i, u[i] = -1, g[i] = 1;
39     for (j = 1; j <= cnt; ++j) {
40       if ((k = i * p[j]) >= N) break;
41       vis[k] = 1;
42       if (i % p[j] == 0) {
43     u[k] = 0, g[k] = u[i];
44     break;
45       }
46       u[k] = -u[i], g[k] = u[i] - g[i];
47     }
48   }
49   for (i = 1; i < N; ++i)
50     g[i] += g[i - 1];
51 }
52  
53 ll work(int n, int m) {
54   ll res = 0;
55   int i, j;
56   for (i = 1; i <= n; i = j + 1) {
57     j = min(n / (n / i), m / (m / i));
58     res += 1ll * (g[j] - g[i - 1]) * (n / i) * (m / i);
59   }
60   return res;
61 }
62  
63 int main() {
64   int T;
65   T = read();
66   get_g();
67   while (T--) {
68     n = read(), m = read();
69     if (n > m) swap(n, m);
70     printf("%lld\n", work(n, m));
71   }
72   return 0;
73 }
View Code

 

BZOJ2820 YY的GCD

标签:

原文地址:http://www.cnblogs.com/rausen/p/4294199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!