The problem of Bzoj2705 Longge

Source: Internet
Author: User
Tags cmath

Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %lld &%llu

Description

Longge is very good at maths and he is very happy to challenge difficult math problems. Now the problem is: Given an integer N, you need to ask for ∑GCD (i, N) (1<=i <=n).

Input

an integer that is n.

Output

an integer, for the answer that is asked.

Sample Input

6

Sample Output

15

Hint

"Data Range"

For 60% of data, 0<n<=2^16.

For 100% of data, 0<n<=2^32.

Source

SDOI2012

Seeking ΣGCD (I,n) (1<=i<=n)

The brute force enumeration is certainly feasible, but tle cannot be avoided.

Considering the conversion problem, from 1 to N, there are many gcd of I (i,n) equal to the same n factor. We can enumerate each factor k of N, and accumulate "the number of gcd (i,n) with the number k as solution multiplied by the number s (k)" To get the answer.

If there is gcd (n,m) =k, then N and M with the exception of the Convention number k, you can get gcd (n/k,m/k) = 1. M/k and (n/k) coprime are known by the preceding formula. The number of (m/k) satisfies the condition, that is, S (k) is equal to Phi (n/k) ← Euler Function!

Solution 1: Direct set of templates. The algorithm is correct, but because the problem data is large, save the function after the re-processing will (the reason for the visual storage with the array can not open so large)

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmaxn=100000;9 Long LongN;Ten intm[maxn],phi[maxn],p[maxn],pt; One intEuler () A { -phi[1]=1; -     intn=MAXN; the     intK; -      for(intI=2; i<n;i++) -     { -         if(!m[i])//I is the prime number +p[pt++]=m[i]=i,phi[i]=i-1; -          for(intj=0;j<pt&& (k=p[j]*i) <n;j++) +         { Am[k]=P[j]; at             if(M[i]==p[j])//in order to ensure that the future number is not re-screened, to break -             { -phi[k]=phi[i]*P[j]; - /*here Phi[k] and Phi[i] behind the ∏ (p[i]-1)/p[i] All the same (M[i]==p[j]) only one p[j], you can guarantee ∏ (p[i]-1)/p[i] in front of the same*/ -                  Break;  -             } in             Else -phi[k]=phi[i]* (p[j]-1);//properties of the integrable function, F (i*k) =f (i) *f (k) to         } +     } - } the intMain () { * Euler (); $scanf"%lld",&n);Panax Notoginseng     Long Longm=sqrt (n); -     inti,j; the     Long Longans=0; +      for(i=1; i<=m;i++){ A         if(n%i==0){ theans+=phi[n/i]*i; +ans+= (n/i) *Phi[i]; -         } $     } $printf"%lld\n", ans); -     return 0; -}

Solution 2: Take a little more time and count it all over again. And not tle, magical

The code was learned from HzW seniors, and it was streamlined.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=100000;9 Long Longn,m;Ten Long LongPhiLong Longx) { One     Long LongA=x; A      for(Long LongI=2; i<=m;i++){ -         if(x%i==0){//find the Factor -a=a/i* (I-1);//Basic Calculation Formula a*= ((i-1)/i) the              while(x%i==0) X/=i;//Remove all the same factors -         } -     } -     if(x>1) a=a/x* (x1);//deal with the last big factor +     returnA; - } + intMain () { Ascanf"%lld",&n); atm=sqrt (n); -     inti,j; -     Long Longans=0; -      for(i=1; i<=m;i++){ -         if(n%i==0){ -Ans+=phi (n/i) *i; inans+= (n/i) *Phi (i); -         } to     } +printf"%lld\n", ans); -     return 0; the}

The problem of Bzoj2705 Longge

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.