Portal
2818:gcd
Time Limit:10 Sec Memory limit:256 MB
submit:3649 solved:1605
[Submit] [Status] [Discuss]
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 Input
4
Sample Output
4
HINT
Hint
For examples (2,2), (2,4), (3,3), (4,2)
1<=n<=10^7
Source
Hubei Province Team Mutual test
Problem Solving Ideas:
The problem is that the number of <=n gcd (x, y) = = primes (2,4) and (4,2) are considered to be different, so we can think of enumerating each prime number so that it gcd (x, y) =p, then we can think of the count of the 1,y/p in [y/p] (where the default Y >X), then we are asking for a Euler function value, then we extend it to the 1-n interval, that is, the Euler function value [1,n/p], but we need to ask for the SIGMAEUALR (n/p) prefix and because Y is taken from the 1-n, so the logarithm is sum[n/p ]*2-1, because it is logarithmic, and there are repeated cases (the case itself is a prime number)
See the code for details:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>usingNamespace Std;typedefLong LongLL;ConstLL MAXN =1e7+5;BOOLPRIME[MAXN];/// tag array is not a prime numberLL PHI[MAXN];/// Euler function values, I Euler value =phi[i]LL P[MAXN]; The value of the element factorLL cnt =0;voidGet_phi ()/// sieve method to find Euler function{cnt =0; memset (Prime,true,sizeof(prime)); phi[1] =1; for(LL i=2; i<maxn; i++)/// Linear sieve method{if(Prime[i])/// Prime{p[cnt++] = i; Phi[i] = i1; The Euler function value of the prime number is prime-1} for(LL j=0; j<cnt; J + +) {if(I*p[j] > MAXN) Break; PRIME[I*P[J]] =false;/// multiples of prime number, so i*p[j] is not a prime number if(I%p[j] = =0)/ //nature: I mod p = = 0, then phi (i * p) = = p * PHI (i){Phi[i*p[j]] = p[j] * Phi[i]; Break; }ElsePHI[I*P[J]] = (p[j]-1) * Phi[i];/// i mod p! = 0, then phi (i * p) = = Phi (i) * (p-1)}}}ll SUM[MAXN]; ///prefix andvoidGet_sum () {memset (sum,0,sizeof(sum)); for(LL i=1; i<maxn; i++) Sum[i] = sum[i-1]+phi[i];}intMain () {Get_phi (); Get_sum (); LL N; while(~SCANF ("%lld", &n)) {LL ans =0; for(LL i=0; i<cnt&&p[i]<=n; i++) {ans = ans+sum[n/p[i]]*2-1; } printf ("%lld\n", ans); }return 0;}
Logarithm of greatest common divisor as prime number in Bzoj 2818:gcd interval (Application of Euler function)