The energy loss for a point (x, y) is (gcd (x, y)-1) * 2 + 1 = gcd (x, y) * 2-1.
Set g (i) to gcd (x, y) = I (1 <= x <= N, 1 <= y <= m) Number of pairs (x, y). This is not good, consider the allowance, set F (i) for the number of pairs containing common factor I (x, y) (1 <= x <= N, 1 <= y <= m) number, obviously f (i) = (n/i) * (m/i). Then g (i) = f (i)-∑f (i * k) (k >= 2, I * k <= min (n, m)).
Then answer =∑ (g (i) * 2-1)
-------------------------------------------------------------------------------------
#include <bits/stdc++.h>using namespace std; typedef long Long ll;const int MAXN = 100009;ll F[MAXN];int main () {ll ans = 0;int n, m;cin >> n >> m;int h = min (n, m);for (int i = h; i; i--) {F[i] = 1LL * (n/i) * (m/i);For (int j = i << 1; J <= h; j + = i)F[i]-= f[j];ans + = f[i] * ((i << 1)-1);}cout << ans << "\ n";return 0;}
-------------------------------------------------------------------------------------
2005: [Noi2010] Energy harvesting time limit: ten Sec Memory Limit: 552 MB
Submit: 2272 Solved: 1358
[Submit] [Status] [Discuss] 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.
Input
Contains only one row, which is two integers n and M.
Output
Contains only an integer that represents the total energy loss generated.
Sample Input"Sample Input 1"
5 4
"Sample Input 2"
3 4
Sample Output"Sample Output 1"
36
"Sample Output 2"
20
"Data size and conventions"
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.
HINT
Source
Math problems
Bzoj 2005: [Noi2010] Energy harvesting (number theory + repulsion principle)