Fzu1969 GCD extreme is similar to uva10561

Source: Internet
Author: User

Description

Given the value of N, you will have to find the value of G. The meaning of G is given in the following code

`G=0; `
`for(i=1;i&ltN;i++)`
`    for(j=i+1;j<=N;j++) `

`        G+=gcd(i,j); `

/* Here gcd () is a function that finds the greatest common divisor of the two input numbers */

Input

The input file contains at most 20000 lines of inputs. each line contains an integer N (1 <n <1000001 ). the meaning of N is given in the problem statement. input is terminated by a line containing a single zero.

Output

For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.

Sample Input

10 100 200000 0

Sample output

67 13015 143295493160

Source

Contest for 2010 lecture II

Question: Give the number N, and calculate the sum of all the number pairs that satisfy 1 <= I <j <= n, and the gcd (I, j) corresponding to (I, j.

Idea: set F (n) = gcd (1, N) + gcd (2, n) + gcd (3, n) +... + Gcd (n-1, n ). Then the answer S (n) = F (2) + f (3) + f (4) +... + F (n ).

Note that the value of all gcd (x, n) is an approximate number of N, so we can classify them according to this approximate number. G (n, I) is used to represent the number of positive integers x that meet gcd (x, n) = I and X <n. Then f (n) = {I × G (n, I) | I is the approximate number of n }. Note that G (n, I) = PHI (n, I ).

If I is enumerated for each n, it will definitely time out according to the data range. We should enumerate I first, and then find the multiple n of I. In this way, the time complexity will be further reduced.

`1/* 2 * Author: Joshua 3 * created time: October 16, Sunday, September 07, 2014 4 * File Name: fzu1969.cpp 5 */6 # include <cstdio> 7 # include <cstring> 8 typedef long ll; 9 # define maxn 100000510 int Phi [maxn], a [maxn], f [maxn]; 11 ll s [maxn]; 12 bool P [maxn]; 13 int tot = 0; 14 15 void PRI () 16 {17 memset (P, 1, sizeof (p); 18 for (INT I = 2; I <maxn; ++ I) 19 if (P [I]) 20 {21 for (Int J = I <1; j <maxn; j + = I) 22 p [J] = false; 23} 24 for (INT I = 2; I <maxn; ++ I) 25 if (P [I]) A [++ tot] = I; 26} 27 28 void phi_table () 29 {30 for (INT I = 2; I <maxn; ++ I) 31 {32 Phi [I] = I; 33 int temp = I; 34 For (Int J = 1; j <= tot; ++ J) 35 {36 IF (temp = 1) break; 37 If (P [temp]) 38 {39 Phi [I]/= temp; 40 Phi [I] * = temp-1; 41 break; 42} 43 If (TEMP % A [J] = 0) 44 {45 Phi [I]/= A [J]; 46 Phi [I] * = A [J]-1; 47 While (TEMP % A [J] = 0) temp/= A [J]; 48} 49} 50} 51} 52 53 void solve () 54 {55 PRI (); 56 phi_table (); 57 for (INT I = 1; I <maxn; ++ I) 58 for (Int J = I; j <maxn; J + = I) 59 f [J] + = I * Phi [J/I]; 60 for (INT I = 1; I <maxn; ++ I) 61 s [I] + = s [I-1] + F [I]; 62} 63 64 int main () 65 {66 int N; 67 solve (); 68 while (scanf ("% d", & N) 69 printf ("% LLD \ n", s [N]); 70 return 0; 71}`

Fzu1969 GCD extreme is similar to uva10561

Related Keywords:

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.