UVA 11426-gcd-extreme (II) Euler function-Mathematics

Source: Internet
Author: User
Tags gcd greatest common divisor

Given the value of N, you'll have the. nd the value of G. The de?nition of G is given below:
G =
I<n

I=1
J

≤n
J=i+1
GCD (i, J)
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
Input numbers*/
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 corresponding
N. The value of G would? t in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160

Test Instructions : Give n, Beg ∑ (i!=j) gcd (i,j) ( 1<=i,j<=n)

The following:s (n) =s (n-1) +gcd (1,n) +gcd (2,n) +......+GCD (n-1,n);

Set f (n) =gcd (1,n) +gcd (2,n) +......+GCD (n-1,n).

GCD (X,n) =i is an approximate (x<n) of N, classified according to this approximate. A constraint that satisfies the GCD (x,n) =i has g (n,i), then f (n) =sum (I*g (n,i)).

The GCD (x,n) =i is equivalent to gcd (x/i,n/i) = 1, so g (n,i) is equivalent to Phi (n/i). Phi (x) is an Euler function.

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd; typedefLong Longll;Const intn=4000000+Ten; ll Phi[n+5], f[n+5];voidphi_table () { for(inti =2; I <= N; i++) Phi[i] =0; phi[1] =1;  for(inti =2; I <= N; i++) {    if(!Phi[i]) {         for(intj = i; J <= N; J + =i) {if(!phi[j]) phi[j] =J; PHI[J]= Phi[j]/I * (i-1); }}}}ll S[n+5],n;intMain () {phi_table ();  for(inti =1; I <= N; i++) {         for(intj = i + i; J <= N; J + =i) {f[j]+ = i * phi[j/i]; }    }     for(inti =1; I <= N; i++) S[i] = s[i-1] +F[i];  while(~SCANF ("%lld",&N)) {if(!n) Break; printf ("%lld\n", S[n]); }    return 0;}
Code

UVA 11426-gcd-extreme (II) Euler function-Mathematics

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.