枚举n的约数d,∑d*phi(d) 就是所求答案,剩下的就是参考别人的证明。
化简 p^i*phi(p^(k-i)) 可得 p^k - p^(k-1) ,注意特判 k==i的情况,注意LL。
#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <cmath> #include <queue> #include <map> #include <set> using namespace std; typedef long long LL; LL gao( LL sum, LL k, LL p) { LL ans = 0; LL t = sum; ans += k*(t - t/p); ans += t; return ans; } int main() { LL n; // n = 1<<31; // cout<<n<<endl; while(scanf("%I64d",&n)!=EOF){ LL t = n; LL ans = 1; for( LL i = 2;i*i<=t;i++){ if(t%i) continue; LL cnt =0; LL sum = 1; while(t%i==0){ t/=i;cnt++;sum*=i; } ans *= gao(sum,cnt,i); } if(t>1) ans*=gao(t,1,t); printf("%I64d ",ans); } return 0; }