Description
The building has a rectangular place where he planted an energy plant capable of collecting sunlight. After these plants collect energy, Dong uses another energy gathering machine to gather the energy these plants collect. Tall plants are neatly planted. There are n columns, each of which has m trees, and the spacing between plants is the same. Therefore, for each plant, the building can be represented by a coordinate (x, y). The range of X is 1 to n, indicating that it is in column X, and the range of Y is 1 to M, indicates y in column X. Because the energy collection machine is large and inconvenient to move, Dong places it on a corner, and the coordinates are exactly (0, 0 ). The energy gathering machine has a certain amount of energy loss in the collection process. If a plant is connected to a power Collecting Machine with K plants on the line segment, the loss of energy is 2 k + 1. For example, when an energy collecting machine collects plants whose coordinates are (2, 4), a 3 energy loss occurs due to a plant (1, 2) in the connection segment. Note: If a plant does not have any plants on the line connecting to the energy collecting machine, the energy loss is 1. Calculate the total energy loss. The following is an example of energy collection, where n = 5, M = 4, a total of 20 plants, the Energy Loss Produced when each plant shows the energy that is collected by machines to collect its energy. In this example, a total of 36 energy losses are generated.
Input
Contains only one row, which is two integers n and M.
Output
Contains only one integer, indicating the total energy loss.
Sample Input
[Example input 1]
5 4
[Example input 2]
3 4
Sample output
[Example output 1]
36
[Sample output 2]
20
[Data scale and Conventions]
10% of data: 1 ≤ n, m ≤ 10;
50% of data: 1 ≤ n, m ≤ 100;
80% of data: 1 ≤ n, m ≤ 1000;
90% of data: 1 ≤ n, m ≤ 10,000;
For 100% of data: 1 ≤ n, m ≤ 100,000.
Finally, I solved the problem.
In fact, the brute force is nlogn. When we calculate the number of GCD = D, it is initially (N/d) * (M/d), then 2 * D, 3 * D, 4 * d .... of
Then the complexity is O (n/1 + n/2 + N/3 + N/4 +... + n/n), which is probably smaller than nlogn.
1 const 2 maxn=1000000; 3 var 4 f:array[0..maxn]of int64; 5 n,m:longint; 6 ans:int64; 7 8 procedure main; 9 var10 i,j,t:longint;11 begin12 read(n,m);13 if n>m then t:=m14 else t:=n;15 for i:=t downto 1 do16 begin17 f[i]:=trunc(n/i)*trunc(m/i);18 j:=i*2;19 while j<=t do20 begin21 dec(f[i],f[j]);22 inc(j,i);23 end;24 inc(ans,f[i]*i);25 end;26 ans:=ans*2-int64(n)*m;27 writeln(ans);28 end;29 30 begin31 main;32 end.
View code