Project Euler 429
题目
Sum of squares of unitary
divisors
A unitary divisor of a number
is a divisor of that has the property .
The unitary divisors of
are and .
The sum of their squares is .
Let represent the sum of
the squares of the unitary divisors of . Thus .
Find .
解决方案
如果一个正整数 分解后成为:
对于 的某个因子 而言,如果 ,那么对于所有 ,都有 。
原因:如果 的一个质因子 的指数 ,也就是 ,那么 对应下的质因子的指数为 ,因此 一定是 的倍数,所以原来的命题成立。
这些因子一共有 个,因为对于所有 , 都有两个选择。因此,这 个因子的平方和为:
另一个问题则是: 中质因子 出现了多少次?
设 是质因子 在 中的次数,那么 ,每一项分别表示 中有多少个数是 的倍数。
最终,将这 个过程相结合,直接计算 的值,其中 。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e8; ll mod=1e9+9; ll qpow(ll n,ll m){ ll ans=1; for(;m;m>>=1){ if(m&1) ans=ans*n%mod; n=n*n%mod; } return ans; } int cal(int n,int p){ int ans=0; for(;n;n/=p) ans+=n/p; return ans; } int pr[N+4],v[N+4],m=0; int main(){ for(int i=2;i<=N;i++){ if(v[i]==0){ v[i]=i;pr[++m]=i; } for(int j=1;j<=m;j++){ if(pr[j]>v[i]||pr[j]>N/i) break; v[i*pr[j]]=pr[j]; } } ll ans=1; for(int i=1;i<=m;i++){ ll cnt=cal(N,pr[i]); ans=ans*(qpow(pr[i],cnt*2)+1)%mod; } printf("%lld\n",ans); }
|