BZOJ2005: [Noi2010] energy harvesting (Euler function)

Description

After another, there is a rectangular piece of land, he planted an energy plant in the ground, this plant can capture the energy of sunlight. After harvesting energy plants,
and much energy pooling then use a machine of the plants collected energy pooled. After another very neat plant species, a total of n columns, each column
includes m trees, plants, vertical pitch are the same, so for each one plant after another can be a coordinate (x, Y) is represented, where x in the range 1 to n,
showing the column in the x, y ranges from 1 to m, represented in the first column of x-y trees. Since the energy collection machine larger, less portable, and much of it will be placed
on a corner, is just coordinate (0, 0). Energy collection machine has a certain energy loss in the process of collection. If an energy plant with a machine together
with a line segment connecting together the k-plant trees, the energy loss is 2k + 1. For example, when the energy collected by a collection machine coordinates (2, 4) of the plant, since
the presence of a plant on the connecting line (1, 2), 3 to produce energy loss. Note that if a plant is not a plant of the energy collection device connected on the line segment
thereof, the energy loss 1. Now to calculate the total energy loss. Here is an example of an energy harvesting, where n = 5, m = 4, a total of 20
Ke plants, indicating a loss of energy occurs when energy collection machine collecting its energy on each plant. In this example, it produced a total of 36 energy
amount loss.

Input

It contains a line for two integers n and m.

Output

Only contains an integer representing a total loss of energy generated.

Sample Input

Sample input [1]

5 4

Sample input [2]

3 4

Sample Output
[1] Sample Output

36

Sample output [2]

20

To 100% of the data: 1 ≤ n, m ≤ 100,000.

Point (i, j) is the loss \ (2 * GCD (i, j) -1 \) , the total loss of all rows and m columns in the n-region is:
\ [\ sum_. 1} ^ {n = I \ sum_ {J}. 1 ^ m = (2 * GCD (I, J) -1) \]
\ [= 2 \ sum_. 1} ^ {n-I = \ sum_ {J} = ^ MGCD. 1 (I, J) -nm \]
means that we need to solve the \ (\ sum_ {i = 1} ^ the n-\ sum_ {J = 1} ^ MGCD (i, J) \) , you can solve this problem.

Because all of the Euler function of a number of factors and is equal to this number.
\ [\ Sum_ {d | n
} \ phi (d) = n \] can be converted into
\ [\ sum_ {i = 1 } ^ n \ sum_ {j = 1} ^ m \ sum_ {d = 1} ^ { min (i, j)} [ d | i and d | j] \ phi (d
) \] on the exchange sequence available
\ [\ sum_ {d = 1 } ^ {min (n, m)} \ sum_ { i = 1} ^ n [d
| i] \ sum_ {j = 1} ^ m [d | j] * \ phi (d) \] since \ (\ sum_ {i = 1 } ^ nd | i = n / d \) , \ (. 1 \) to the \ (n-\) in d can have a divisible \ (n / d \) a, so the equation is equal to
\ [\ sum_ {d = 1 } ^ {min (n, m)} (n / d)
* (m / d) * \ phi (d) \] we only need pre-treatment \ (1E5 \) Euler function to within.
Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
ll phi[N];
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        phi[i]=i;
    }
    for(int i=2;i<=n;i++)
    {
        if(phi[i]==i)
        {
            for(int j=i;j<=n;j+=i)
            {
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}
int main()
{
    ll n,m;
    ll ans=0;
    cin>>n>>m;
    init(1e5);
    for(ll d=1;d<=min(n,m);d++)
    {
        ans+=(n/d)*(m/d)*phi[d];
    }
    ans=ans*2;
    ans-=n*m;
    cout<<ans<<"\n";
    return 0;
}

Guess you like

Origin www.cnblogs.com/hh13579/p/11790116.html