bzoj2705

一个常用的结论(方法)

只要知道gcd(i,n)=L 的i的个数s,我们就能很轻易得出答案

gcd(i,n)=L

gcd(i/L,n/L)=1  

不难得到这样的s=与n/L互质的个数=phi(n/L)

一个数的欧拉函数最坏情况是可以在O(sqrt(n))的复杂度中弄出来的

我们可以穷举L,只要从1穷举到根号n即可

 1 var i:longint;
 2     ans,n:int64;
 3 
 4 function phi(x:int64):int64;
 5   var i:longint;
 6   begin
 7     phi:=1;
 8     for i:=2 to trunc(sqrt(n)) do
 9       if x mod i=0 then
10       begin
11         phi:=phi*(i-1);
12         x:=x div i;
13         while x mod i=0 do
14         begin
15           x:=x div i;
16           phi:=phi*i;
17         end;
18         if x=1 then break;
19       end;
20     if x>1 then phi:=phi*(x-1);
21   end;
22 
23 begin
24   readln(n);
25   for i:=1 to trunc(sqrt(n)) do
26     if n mod i=0 then
27     begin
28       ans:=ans+phi(n div i)*i;
29       if i<>n div i then ans:=ans+phi(i)*(n div i);
30     end;
31   writeln(ans);
32 end.
View Code
原文地址:https://www.cnblogs.com/phile/p/4473169.html