# BZOJ2005: [Noi2010]能量采集

## 2005: [Noi2010]能量采集

Time Limit: 10 Sec  Memory Limit: 552 MB
Submit: 4174  Solved: 2494
[Submit][Status][Discuss]

【样例输入1】
5 4
【样例输入2】
3 4

【样例输出1】
36
【样例输出2】
20

## Source【题解】

http://www.cnblogs.com/huhuuu/archive/2011/11/25/2263803.html

1、考虑f[x]为gcd（i,j）含有因数x的对数，减去最大公因数是2x,3x....的即可http://hzwer.com/3482.html

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #define min(a, b) ((a) < (b) ? (a) : (b))
6
7 inline void read(long long &x)
8 {
9     x = 0;char ch = getchar(), c = ch;
10     while(ch < '0' || ch > '9')c = ch, ch = getchar();
11     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
12     if(c == '-')x = -x;
13 }
14
15 const long long MAXN = 100000 + 10;
16
17 long long f[MAXN << 1], n, m, ans;
18
19 int main()
20 {
22     int mi = min(n,m);
23     for(register long long i = mi;i;-- i)
24     {
25         f[i] = (n/i) * (m/i);
26         for(register long long j = (i << 1);j <= mi;j += i)
27             f[i] -= f[j];
28         ans += (f[i] << 1) * i - f[i] ;
29     }
30     printf("%lld", ans);
31     return 0;
32 }
BZOJ2005

2、，莫比乌斯反演http://blog.csdn.net/Clove_unique/article/details/51089272

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #define min(a, b) ((a) < (b) ? (a) : (b))
6
7 inline void read(long long &x)
8 {
9     x = 0;char ch = getchar(), c = ch;
10     while(ch < '0' || ch > '9')c = ch, ch = getchar();
11     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
12     if(c == '-')x = -x;
13 }
14
15 const long long MAXN = 100000 + 10;
16
17 long long f[MAXN << 1], n, m, ans, mi, phi[MAXN], bprime[MAXN], prime[MAXN], tot;
18
19 void make_phi()
20 {
21     phi[1] = 1;
22     for(register long long i = 2;i <= mi;++ i)
23     {
24         if(!bprime[i])
25         {
26             prime[++tot] = i;
27             phi[i] = i - 1;
28         }
29         for(register long long j = 1;j <= tot && i * prime[j] <= mi;++ j)
30         {
31             bprime[i * prime[j]] = 1;
32             if(i % prime[j] == 0)
33             {
34                 phi[i * prime[j]] = phi[i] * prime[j];
35                 break;
36             }
37             phi[i * prime[j]] = phi[i] * (prime[j] - 1);
38         }
39     }
40     for(register long long i = 1;i <= mi;++ i) phi[i] += phi[i - 1];
41 }
42
43 int main()
44 {
46     mi = min(n, m);
47     make_phi();
48     register long long x,y,last;
49     for(register long long d = 1;d <= mi;++ d)
50     {
51         x = n/d, y = m/d;
52         last = min(n/x, min(m/y, mi));
53         ans += (phi[last] - phi[d - 1]) * x * y;
54         d = last;
55     }
56     printf("%lld", 2 * ans - n * m);
57     return 0;
58 }
BZOJ2005

posted @ 2017-09-07 16:21  嘒彼小星  阅读(268)  评论(0编辑  收藏  举报