【BZOJ2818】Gcd (欧拉函数)

时间:2023-03-09 07:22:19
【BZOJ2818】Gcd (欧拉函数)

  网址:http://www.lydsy.com/JudgeOnline/problem.php?id=2818

  一道数论裸题,欧拉函数前缀和搞一下就行了。

  小于n的gcd为p的无序数对,就是phi(1~n/p)的和,因为如果gcd(x,y)=p那么必有gcd(x/p,y/p)=1

  转化成有序数对就可以把无序数对的个数*2-1(减1是因为有一个数对是(p,p))

代码:

var p,phi:array[..]of longint;
s:array[..]of int64;
b:array[..]of boolean;
n,m,i,j,k,t:longint;
ans:int64;
begin
read(n); t:=;
for i:= to n do b[i]:=true;
b[]:=false; phi[]:=;
for i:= to n do begin
if b[i] then begin
phi[i]:=i-; inc(t); p[t]:=i;
end;
for j:= to t do begin
if i*p[j]>n then break;
b[i*p[j]]:=false;
if i mod p[j]= then begin
phi[i*p[j]]:=phi[i]*p[j];
break;
end;
phi[i*p[j]]:=phi[i]*phi[p[j]];
end;
end;
ans:=;
for i:= to n do
s[i]:=s[i-]+phi[i];
for i:= to t do
ans:=ans+s[n div p[i]]*-;
writeln(ans);
end.