[BZOJ2818] Gcd

Gcd

Time Limit: 10 Sec Memory Limit: 256 MB

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

对于样例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

Source

湖北省队互测

题解

  • gcd(x,y)=k,k
  • gcd(xk,yk)=1

  • k,ans=f[yk],f[i]i,f[i]=φ(i)

  • ans=ans2
  • (2,2)(3,3)(5,5)...ans=anslen
  • x=1ny=1n[gcd(x,y)=P]=x=1n/Py=1n/P[gcd(x,y)=1]=2i=2n/P[ϕ(i)+1]
var
 check:array[0..10000005]of boolean;
 phi,prime:array[0..10000005]of longint;
 sum:array[0..10000005]of int64;
 i,j,len,n:longint;
 ans:int64;
begin
 readln(n); len:=0; phi[1]:=1;
 for i:=2 to n do
  begin
   if check[i]=false
   then begin inc(len); prime[len]:=i; phi[i]:=i-1; end;
   for j:=1 to len do
    begin
     if i*prime[j]>n
     then break;
     check[i*prime[j]]:=true;
     if i mod prime[j]=0
     then begin phi[i*prime[j]]:=phi[i]*prime[j]; break; end
     else phi[i*prime[j]]:=phi[i]*(prime[j]-1);
    end;
  end;
 for i:=1 to n do
  sum[i]:=sum[i-1]+phi[i];
 ans:=0;
 for i:=1 to len do
  inc(ans,sum[n div prime[i]]);
 ans:=ans*2-len;
 writeln(ans);
end.

你可能感兴趣的:([BZOJ2818] Gcd)