# 51nod 1040 Greatest Common divisor (mathematics)

Source: Internet
Author: User

Give an n, ask 1-n this n number, and the greatest common divisor of N. For example: n = 6
1,2,3,4,5,6 with 6 of the greatest common divisor respectively for 1,2,3,2,1,6, plus = 15
Input

1 number N (n <= 10^9)

OutPut

Sum of the number of conventions

Input example

6

Output example

15

AC Code:

``/** * @xiaoran 2 3 4 5 6 * 2 3 2 1 6 * 2 1, 2 2, one 3, one 6, note that the following values are the factor of N. * Now we just have to figure out the number of factors, * then the number of 1 is the number of n coprime, that is, the value of the Euler function K=phi (N/2), * for the number of n factor i K=phi (n/i), sum+=k*i, */#include <iostream>#include <cstdio>#include <map>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <cstdlib>#include <cctype>#include <cmath>#include <ctime>#define LOCAL#define LL Long Longusing namespace STD; LL Phi (intN) {//Find Euler function    if(n==1)return 1; LL ans=n;intK= (int)sqrt(n+0.5); for(intI=2; i<=k;i++) {if(n%i==0) {ans=ans* (i1)/I; } while(n%i==0) N/=i; }if(n>1) ans=ans* (n1)/n;returnAns;}intgcdintAintb) {intRif(b==0)returnA while(b)        {r=a%b;        A=b;    B=r; }returnA;}intPK (intKintN) {ints=1; for(inti=k*2; i<n;i+=k) {//Calculate the number of K        if(GCD (n,i) >k)Continue;    s++; }returns;}intMain () {/* #ifdef LOCAL freopen ("Input.txt", "R", stdin);    Freopen ("Output.txt", "w", stdout); #endif * /    intN while(Cin>>n) {LL t1=clock ();intK= (int)sqrt(n+0.5); LL ans=n; for(intI=2; i<=k;i++) {if(n%i==0){//cout<<pk (i,n) << "" <<PK (n/i,n) <<endl;Ans+=phi (n/i) *i+ (n/i) *phi (i); }} ans+=phi (n);if(k*k==n)        {Ans-=k*phi (k); }cout<<ans<<endl; LL T2=clock ();//cout<<t2-t1<<endl;}return 0;}``

51nod 1040 Greatest Common divisor (mathematics)

Related Keywords:

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.