BZOJ2005: [Noi2010]能量采集

2005: [Noi2010]能量采集

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

Description

栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量。在这些植物采集能量后,
栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起。 栋栋的植物种得非常整齐,一共有n列,每列
有m棵,植物的横竖间距都一样,因此对于每一棵植物,栋栋可以用一个坐标(x, y)来表示,其中x的范围是1至n,
表示是在第x列,y的范围是1至m,表示是在第x列的第y棵。 由于能量汇集机器较大,不便移动,栋栋将它放在了
一个角上,坐标正好是(0, 0)。 能量汇集机器在汇集的过程中有一定的能量损失。如果一棵植物与能量汇集机器
连接而成的线段上有k棵植物,则能量的损失为2k + 1。例如,当能量汇集机器收集坐标为(2, 4)的植物时,由于
连接线段上存在一棵植物(1, 2),会产生3的能量损失。注意,如果一棵植物与能量汇集机器连接的线段上没有植
物,则能量损失为1。现在要计算总的能量损失。 下面给出了一个能量采集的例子,其中n = 5,m = 4,一共有20
棵植物,在每棵植物上标明了能量汇集机器收集它的能量时产生的能量损失。 在这个例子中,总共产生了36的能
量损失。

Input

仅包含一行,为两个整数n和m。

Output

仅包含一个整数,表示总共产生的能量损失。

Sample Input

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

Sample Output

【样例输出1】
36
【样例输出2】
20
对于100%的数据:1 ≤ n, m ≤ 100,000。

HINT

Source

【题解】

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

最终答案2ΣΣ(gcd(i,j) - 1)

两种做法。

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 {
21     read(n), read(m);
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 {
45     read(n), read(m);
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
原文地址:https://www.cnblogs.com/huibixiaoxing/p/7490395.html