CodeVS1937 Energy Harvesting
2010 NOI National Competition
Title Description
Description
The building has a rectangular land, he planted an energy plant on the ground, which can collect the energy of sunlight. After the plants collected energy, the building then used an energy pooling machine to bring together the energy collected by the plants.
The plants in the building are very neatly planted, there are n columns, each of which has m, and the plant's spacing is the same, so for each plant, the building can be represented by a coordinate (x, y), where the range of x is 1 to n, that is in column X, the range of Y is 1 to M, and that is the nth tree in column X.
Because the energy collection machine is large and inconvenient to move, the building puts it on a corner with coordinates exactly (0, 0).
The energy pooling machine has a certain energy loss during the collection process. If a plant is connected to an energy collection machine with a K plant on its line, the loss of energy is 2k + 1. For example, when the energy collection machine collects plants with coordinates of (2, 4), there is a 3 loss of energy due to the presence of a plant (1, 2) on the connecting segment. Note that if a plant has no plants on the line segment connected to the energy pooling machine, the energy loss is 1. Now we have to calculate the total energy loss.
An example of energy harvesting is given below, where n = 5,m = 4, a total of 20 plants, showing the energy loss generated by the energy collection machine on each plant.
In this example, a total of 36 of the energy loss is generated.
Enter a description input
Description
The input file energy.in contains only one row, which is two integers n and M.
outputs description output
Description
The output file Energy.out contains only an integer that represents the total energy loss generated.
sample input to
sample"Sample Input 1"
5 4
"Sample Input 2"
3 4
Sample output Sample
outputs"Sample Output 1"
36
"Sample Output 2"
20
data
size & Hint
For 10% data: 1≤n, m≤10;
For 50% data: 1≤n, m≤100;
For 80% data: 1≤n, m≤1000;
For 90% data: 1≤n, m≤10,000;
For 100% data: 1≤n, m≤100,000.
tested by AcceptedTotal time: AMs0/0The data passes the test.Run Results
test point #energy1.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy10.in results:ACMemory usage: 1004kB time consumption: 3ms
test point #energy2.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy3.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy4.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy5.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy6.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy7.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy8.in results:ACMemory usage: 256kB time consumption: 1ms
test point #energy9.in results:ACMemory usage: 256kB time consumption: 1ms
AnalysisFirst, a small conclusion:for a point (x, y) on a Cartesian coordinate system, a point with a positive integer from (0,0) to (x, y) is gcd (x, y)Proof: http://blog.csdn.net/fsahfgsadhsakndas/article/details/51346126The energy of each plant in this problem is 2k+1, because it does not contain itself so K=GCD (x, y)-1,so ans=∑[gcd (i,j) *2-1]to convert, we enumerate I=GCD (x, y),Note F[i] is "gcd (x, y) =i number of points", that is "how many pairs (x, y) make gcd (x, y) =i"when the range of x and Y is the same, we can use the Euler function to figure out the valuenow, however, the X and Y ranges are N and M, respectively, and the Euler function fails.This method is seen on the hzwer:f[i]= (n/i) (m/i)-f[ki] (K<=min (n,m))application of the Repulsionthe principle is simple, (n/i) (m/i) indicates how many pairs (x, y) make d|gcd (x, y), then this contains gcd=d,2d,3d .... [The number of n/d]dAll we need is a gcd=d, just minus those 2d,3d .when I=min (n,m), it can be found that f[min (n,m)] is correct, from F[min (n,m)] to start the previous push to get all the f[i]It's amazing, isn't it? This equation is very understood, but it is difficult to understand that HzW said its time complexity is O (NLOGN), so I wrote a small program,Calculation (1+1/2+1/3+ ... 1/n), compare it with LOG2 (n) and discover when n=10 (1+1/2+1/3+ ... 1/10)/logn=1.13, and when the n=10^8, the ratio is 1.39, I also output the median value, found that the ratio is monotonically increasing,so we can say
within 1 to 10^8, it is approximately considered (1+1/2+1/3+ ... 1/n) =logn The time complexity of calculating the equation is O (nlogn)back to Title: Ans=f[x] (2*x-1), the complexity is O (n)Code
CodeVS1937 Energy harvesting noi2010#include <cstdio> #include <algorithm> #define MAXN 1000010#define ll Long longusing Namespace Std;ll N, M, ans, F[MAXN], M;int main () {ll I, J, x;scanf ("%ld%ld", &n,&m), M=min (n,m); for (i=m;i;i--) {f[i ]= (n/i) * (m/i); for (j= (i<<1); j<=m;j+=i) f[i]-=f[j];} for (i=1;i<=m;i++) ans+=f[i]* (2*i-1);p rintf ("%lld\n", ans); return 0;}
NOI2004 Energy Harvesting