Toss a night very water of number theory, the whole people are Meng Meng da
The algorithm of O (n) of Euler sieve and prime sieve is mainly seen.
The advantage of this algorithm, which is longer than that of a long list of English names, is that a number is not counted more than once, which means that a number is sieved once, mainly after%==0, and the specific explanation of the confusion, especially the solution of Euler's function.
http://blog.csdn.net/lerenceray/article/details/12420725
The code is as follows
1 voidES () {2 for(intI=2; i<n;i++){3 if(!Pd[i]) {4prime[++top]=i;5phi[i]=i-1;6 }7 for(intj=1; j<=top&&prime[j]*i<=n;j++){8pd[prime[j]*i]=1;9 if(i%prime[j]==0){Tenphi[prime[j]*i]=phi[i]*Prime[j]; One Break; A } -phi[prime[j]*i]=phi[i]* (prime[j]-1); - } the } -}
By the way, a dropped two number-theoretic water problems.
Description
Given an integer n, 1<=x,y<=n and gcd (x, y) are prime
Number pairs (x, y) how many pairs.
Input
An integer n
Output
Title
Sample Input4
Sample Output4HINT
Hint
For examples (2,2), (2,4), (3,3), (4,2)
1<=n<=10^7
Solving
GCD (A, B) =p is gcd (a/p,b/p) = 1;
Set A<=b gcd (A, b) =phi[b] (number of coprime)
So we only need to enumerate each prime, each number (here can be used to maintain the prefix of N, recorded as F[n]) to solve the ans=σf[n/prime[i]]*2-1 (consider a>b, and subtract values such as ().
#include <cstdio>Const intmaxn=10000010;BOOLPD[MAXN];Long LongPhi[maxn],prime[maxn],top,n,ans;voidES () { for(intI=2; i<n;i++){ if(!Pd[i]) {prime[++top]=i; Phi[i]=i-1; } for(intj=1; j<=top&&prime[j]*i<=n;j++) {Pd[prime[j]*i]=1; if(i%prime[j]==0) {Phi[prime[j]*i]=phi[i]*Prime[j]; Break; } Phi[prime[j]*i]=phi[i]* (prime[j]-1); } } }intMain () {scanf ("%lld",&N); ES (); for(intI=1; i<n;i++) phi[i]+=phi[i-1]; for(intI=1; i<=top;i++) ans+=phi[n/prime[i]]*2+1; printf ("%lld", ans);}
Description
As a sports Commissioner, C June is responsible for the training of the honor guard. Guard of Honor is composed of students of N * N of the square, in order to ensure that the procession uniform, c June will follow the guard of Honor's left rear, according to the number of students in the line of sight to determine whether the team is neat (such as). Now, C-June wants you to tell him the number of students you can see when the team is neat.
Input
A total of a number n.
Output
A total of a number, that is, c June should see the number of students.
Sample Input4
Sample Output9
HINT
"Data size and conventions" for 100% of data, 1≤n≤40000
The ... Find Phi, calculate
1#include <cstdio>2 Const intmaxn=40010;3 BOOLPD[MAXN];4 intPhi[maxn],prime[maxn],top,n,ans;5 voidES () {6 for(intI=2; i<n;i++){7 if(!Pd[i]) {8prime[++top]=i;9phi[i]=i-1;Ten } One for(intj=1; j<=top&&prime[j]*i<=n;j++){ Apd[prime[j]*i]=1; - if(i%prime[j]==0){ -phi[prime[j]*i]=phi[i]*Prime[j]; the Break; - } -phi[prime[j]*i]=phi[i]* (prime[j]-1); - } + } - } + A intMain () { atscanf"%d",&n); - ES (); - for(intI=1; i<n;i++) phi[i]+=phi[i-1]; -ans=phi[n-1]*2+3; -printf"%d", ans); -}
Prime Sieve && Euler sieve BZOJ2818 Gcd BZOJ2190 [SDOI2008] honor Guard