POJ2480
題意:Given an integer N(1<N<2^31),you are to calculate ∑gcd(i, N) 1<=i<=N.
分析:數論題的魅力就在于此,一道題就一句簡單的描述.
顯然,最后的答案肯定是由N的約數組成的,于是我們試著統計每個約數被用了幾次.
對于N的一個約數D,有多少個數與N的gcd是D呢?答案是φ(N/D).
于是ans=∑φ(N/D)*D==∑φ(D)*(N/D). (D為N的約數)
效率為O(sqrt(N)).
code:
var p,cnt:array[0..15] of longint;n,m,num,i:longint;tmp,ans:extended;procedure dfs(now:extended; k:longint);var x,y:longint;beginif k>m thenbeginans:=ans+now*n;exit;end;for x:=0 to cnt[k] dobeginif x<>0 then now:=now*(1-1/p[k]);dfs(now,k+1);if x<>0 then now:=now/(1-1/p[k]);end;end;beginwhile not seekeof dobeginreadln(n);num:=n;m:=0;fillchar(cnt,sizeof(cnt),0);for i:=2 to trunc(sqrt(n)) doif num mod i=0 thenbegininc(m);p[m]:=i;while num mod i=0 dobegininc(cnt[m]);num:=num div i;end;if num=1 then break;end;if num>1 thenbegininc(m);p[m]:=num;cnt[m]:=1;end;ans:=0;dfs(1,1);writeln(ans:0:0);end; end.轉載于:https://www.cnblogs.com/exponent/archive/2011/08/13/2137219.html
總結
- 上一篇: Windows驱动开发技术详解笔记
- 下一篇: 关于HTML5中Canvas的宽、高设置