topic Link: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&itemid=8&page=show_problem&category=473&problem=2421&mosmsg= submission+received+with+id+13800900
Given the value of N, you'll have the. nd the value of G. The de?nition of G is given below:
Input
The input Le contains at most lines of inputs. Each line contains an integer N (1 < N < 4000001).
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 CORRESPONDINGN. The value of G would? t in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
We assume that B[n] represents 1 to n-1 with the GCD of N and, then g[n]=g[n-1]+b[n];
A[i] Represents the number of x with GCD (n, x) = i, b[n]=sum (a[i] * i), so we only need a[i], according to GCD (n, x) =i----->gcd (n/i, x/i) = 1,
So just ask for the Euler function Phi (n/i), you can get the number of coprime with n/i, and then find the number of gcd (x, n) = i, so that the overall can solve the
#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespacestd;#defineN 4000001typedefLong LongLL; LL A[n], b[n], dp[n];intMain () { for(intI=2; i<n; i++)///Euler dozen tables; { if(!A[i]) { for(intJ=i; j<n; j+=i) {if(!a[j]) a[j]=J; A[J]=a[j]/i* (I-1); } } } for(intI=1; i<n; i++)///all the numbers in [1,n-1] and the GCD of n for(intj=i*2; j<n; j+=i) b[j]+ = a[j/i]*i; for(intI=2; i<n; i++) Dp[i]=dp[i-1]+B[i]; intN; while(SCANF ("%d", &N) {printf ("%lld\n", Dp[n]); } return 0;}
View Code
Application of Euler function in UVA11426 gcd-extreme (II)---