UVA 11426 GCD Summation

Source: Internet
Author: User
Tags greatest common divisor

UVA 11426 GCD summation O-gcd-extreme (II) Time Limit:10000MS Memory Limit:0KB 64bit IO Format:%LLD &%llusubmit Status Practice UVA 11426


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:


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.


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.


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 64-bit signed integer.

Sample input Output for sample input








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 (n-1) + [i=1~n-1 sum]gcd (n,i); make F (n) = [i=1~n-1 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, maxn-1){        if(!Euler[i]) {Repp (J,I,MAXN-1, I) {                if(!euler[j]) euler[j]=J; EULER[J]=euler[j]/i* (I-1); }        }    }}intMain () {Geteuler ();    MS0 (f); REP (i,1, maxn-1) {Repp (j,i*2, maxn-1, i) f[j]+=i*euler[j/i];    } MS0 (F); REP (i,1, maxn-1) f[i]=f[i-1]+F[i];  while(cin>>n,n) {cout<<F[n]<<Endl; }    return 0;}
View Code

UVA 11426 GCD Summation

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.