Application of Euler function in UVA11426 gcd-extreme (II)---

Source: Internet
Author: User
Tags gcd

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)---

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.