Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36402
Test instructions: On a rectangular ground, plant a kind of energy plant, plants are very neat, there are n columns, each column has M tree, each plant corresponds to a coordinate (x, y). At (0, 0) There is an energy collector, each plant to the energy collector has a loss of energy, if a plant and energy collection machine connected to the line of K plant, then the loss of energy is 2k + 1, if a plant and energy collection machine connected to the line segment without plants, then the energy loss is 1. To seek total energy loss.
Idea: f[d]
(x, y) = d
the logarithm of the order, then the answer issigma(f[i]*((i-1)*2+1))。分两步来理解这个结论,if gcd(x1, y1) = gcd(x2, y2)那么他们的能量损失一样多(与能量汇集机器连接而成的线段上的植物一样多),再来看看具体的能量损失(也就是如何判断连接线段上的植物有多少棵),那么显然与gcd的值有关,结果就是2 * gcd - 1。
Code
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5typedefLong LongLL;6 Const intMAXN =100005;7LL F[MAXN];//F[i] Indicates a logarithm that satisfies gcd (x, y) = I (1 <= x <= N, 1 <= y <= m)8 9 intMain ()Ten { One LL N, m; A while(SCANF ("%lld%lld", &n, &m)! =EOF) { - if(N >m) Swap (n, m); -LL ans =0; the for(inti = n; I >=1; --i) { -F[i] = (n/i) * (M/i); - for(intj = i + i; J <= N; J + =i) { -F[i]-=F[j]; + } -Ans + = f[i] * (2I1);//all gcd (x, y) = I, resulting in a loss of energy . + } Aprintf"%lld\n", ans); at } - return 0; -}
Bzoj 2005 Energy Harvesting