UVA 11426 GCD summation Ogcdextreme (II)
Time Limit:10000MS
Memory Limit:0KB
64bit IO Format:%LLD &%llusubmit Status Practice UVA 11426
Description
Problem J
GCD Extreme (II)
Input: Standard Input
Output: Standard Output
Given the value of N, you'll have to find the value of G. The definition of G is given below:
Here GCD (i,j) means the greatest common divisor of integer i and integer J.
For those who has trouble understanding summation notation, the meaning of G is given in the following code:
g=0; for (i=1;i<n;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 The. 
Input
The input file 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 corresponding N. The value of G would fit in a 64bit signed integer.
Sample input Output for sample input
10 100 200000 0

67 13015 143295493160

Problemsetter : Shahriar Manzoor
Special Thanks:syedmonowarhossain
Test instructions: For a given n, output 1~n all the different numbers of the number pairs of gcd and ideas: Set the answer to F (n) obviously f (n) =f (n1) + [i=1~n1 sum]gcd (n,i); make F (n) = [i=1~n1 sum]gcd (n,i); to GCD (n,i ) =x, (I<n), X is an approximate number of n (x<n) then gcd (n/x,i/x) =1; (x is an approximate number of n) then the numbers of I that meet the condition are S (n,x) =euler[n/x] that is less than n/i and the numbers of n/i coprime are the Euler function f (n) =[ ]x*s (n,x) =[]x*euler[n/x] ([] for summation) and then according to the method of the prime number sieve each I sieve out 1~MAXN f[i].
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<cctype>#definell Long Long#defineREP (I,A,B) for (int (i) = (a);(i) <= (b);(i) + +)#defineRepp (i,a,b,t) for (int (i) = (a);(i) <= (b);(i) + = (t))#definePII pair<int,int>#defineMP Make_pair#definePB push_back#defineRI (x) scanf ("%d",& (x))#defineRLL (x) scanf ("%lld",& (x))#defineRI64 (x) scanf ("%i64d",& (x))#defineDRI (x) int x;scanf ("%d",& (x))#defineDrll (x) ll x;scanf ("%lld",& (x))#defineDRI64 (x) llx;scanf ("%i64d",& (x))#defineMS0 (a) memset ((a), 0,sizeof ((a)))#defineMS1 (a) memset ((a), 0,sizeof ((a)))#defineMS (b) memset ((a), (b), sizeof ((a)))using namespacestd;Const intmaxn=4000100;Const intInf= (1<< in);Const Doubleeps=0.0000000001;Const DoublePi=acos (1.0); ll N;intEuler[maxn];ll F[MAXN],F[MAXN];voidGeteuler () {MS0 (Euler); euler[1]=1; REP (i,2, maxn1){ if(!Euler[i]) {Repp (J,I,MAXN1, I) { if(!euler[j]) euler[j]=J; EULER[J]=euler[j]/i* (I1); } } }}intMain () {Geteuler (); MS0 (f); REP (i,1, maxn1) {Repp (j,i*2, maxn1, i) f[j]+=i*euler[j/i]; } MS0 (F); REP (i,1, maxn1) f[i]=f[i1]+F[i]; while(cin>>n,n) {cout<<F[n]<<Endl; } return 0;}
View Code
UVA 11426 GCD Summation