"Bzoj" 2005: [Noi2010] energy harvesting (Euler functions + chunking)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2005

First and a topic should be the same as you can see each point on the line with gcd (x, y)-1 points blocked their own ...

Then there is the request:

$$\sum_{x=1}^{n} \sum_{y=1}^{m} 2 \times ((x, y)-1) + 1$$

We can get a formula.

$$-n*m + 2\sum_{x=1}^{n} \sum_{y=1}^{m} (x, y) $$

Then the right one is the bare chunk + Euler function. No, please look at my original blog post ...

Then notice the opening longlong.

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int N=100005;int P[n], CNT, np[n], Phi[n];ll sum[n];void init (int n) {Phi[1]=1;for1 (I, 2, n) {if (!np[i]) p[++cnt]=i, Phi[i]=i-1;for1 (J, 1, CNT) {int t=p[j]*i; if (t>n) break;np[t] =1;if (i%p[j]==0) {phi[t]=phi[i]*p[j]; break;} PHI[T]=PHI[I]*PHI[P[J]];}} For1 (i, 1, N) sum[i]=sum[i-1]+phi[i];} int main () {int n=getint (), M=getint (), if (n>m) Swap (n, m), Init (n), int pos;ll ans=0;for (int i=1; i<=n; i=pos+1) {pos= Min (n/(n/i), m/(m/i)), ans+= (Sum[pos]-sum[i-1]) * (n/i) * (m/i);} printf ("%lld\n", (ans<<1)-(ll) n*m); return 0;}

  

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 "Example 1"
5 4


"Sample Input 2"
3 4

Sample Output "Example 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 (Euler functions + chunking)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.