bzoj 2705 欧拉函数
2017-04-20 18:07
211 查看
题意:给定一个n,求sigma(gcd(i,n))(1<=i<=n)
显然,sigma(gcd(i,n))=sigma(g*f[g]) (n mod g=0) ,其中f[g]表示gcd(i,n)=g 的个数
g可以通过O(sqrt(n))的时间枚举出来,关键是求f[g]
f[g]=sigma(1) (gcd(i,n)=g)
=sigma(1) (gcd(i/g,n/g)=1) (“/”表示整除)
易知 i/g<=n/g
所以f[g]实际上就是phi(n/g)
所以 ans=sigma(g*phi(n/g)) (n mod g=0)
看一下数据范围会发现线性筛预处理会跪掉,所以每一次用欧拉函数的公式现算一下..
var
ans,n :qword;
i :longint;
function find(x:qword):qword;
var
ans:qword;
p:longint;
begin
ans:=x;
for p:=2 to trunc(sqrt(x)) do
if (x mod p=0) then
begin
ans:=ans*(p-1) div p;
while (x mod p=0) do x:=x div p;
end;
if x>1 then ans:=ans*(x-1) div x;
exit(ans);
end;
begin
read(n);
ans:=0;
for i:=1 to trunc(sqrt(n)) do
if (n mod i=0) then
begin
inc(ans,find(i)*(n div i));
if i<>n div i then inc(ans,find(n div i)*i);
end;
writeln(ans);
end.
——by Eirlys
显然,sigma(gcd(i,n))=sigma(g*f[g]) (n mod g=0) ,其中f[g]表示gcd(i,n)=g 的个数
g可以通过O(sqrt(n))的时间枚举出来,关键是求f[g]
f[g]=sigma(1) (gcd(i,n)=g)
=sigma(1) (gcd(i/g,n/g)=1) (“/”表示整除)
易知 i/g<=n/g
所以f[g]实际上就是phi(n/g)
所以 ans=sigma(g*phi(n/g)) (n mod g=0)
看一下数据范围会发现线性筛预处理会跪掉,所以每一次用欧拉函数的公式现算一下..
var
ans,n :qword;
i :longint;
function find(x:qword):qword;
var
ans:qword;
p:longint;
begin
ans:=x;
for p:=2 to trunc(sqrt(x)) do
if (x mod p=0) then
begin
ans:=ans*(p-1) div p;
while (x mod p=0) do x:=x div p;
end;
if x>1 then ans:=ans*(x-1) div x;
exit(ans);
end;
begin
read(n);
ans:=0;
for i:=1 to trunc(sqrt(n)) do
if (n mod i=0) then
begin
inc(ans,find(i)*(n div i));
if i<>n div i then inc(ans,find(n div i)*i);
end;
writeln(ans);
end.
——by Eirlys
相关文章推荐
- bzoj 2705: [SDOI2012]Longge的问题 欧拉函数
- bzoj2705 [SDOI2012]Longge的问题 欧拉函数
- 【BZOJ 2705】[SDOI2012]Longge的问题 欧拉函数
- BZOJ 2705([SDOI2012]Longge的问题-欧拉函数φ(i))
- [BZOJ 2705][SDOI 2012]Longge的问题(欧拉函数)
- [bzoj] 2705 Longge的问题 || 欧拉函数
- [欧拉函数]Bzoj2705 Longge的问题[SDOI2012]
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
- 【bzoj2705】【sdoi2012】【longge的问题】【欧拉函数】
- BZOJ 2705 SDOI2012 Longge的问题 因数分解+欧拉函数
- bzoj2705 欧拉函数
- 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题
- BZOJ 2705 - 经典问题 欧拉函数
- 【BZOJ 2705】【SDOI 2012】Longge的问题【欧拉函数】
- BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)
- bzoj--2705--Longge的问题(欧拉函数)
- 【bzoj】2705 - [SDOI2012]Longge的问题(欧拉函数)
- bzoj 2705: [SDOI2012]Longge的问题(欧拉函数)
- [BZOJ2705][SDOI2012]Longge的问题(欧拉函数)
- bzoj2705 [SDOI2012]Longge的问题(欧拉函数)