銆怭OJ銆?480 Longge's problem锛堟鎷夊嚱鏁帮級
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怭OJ銆?480 Longge's problem锛堟鎷夊嚱鏁帮級相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/can' title='can'>can
color scan href 浠g爜 std ble main org棰樼洰
浼犻€侀棬锛歈WQ
鍒嗘瀽
棰樻剰灏辨槸姹?sum;gcd(i, N) 1<=i <=N.銆?/p>
鏄剧劧$ gcd(i,n) = x $鏃讹紝蹇呯劧$x|n$銆?/p>
鎵€浠ユ垜浠灇涓句竴涓媙鐨勭害鏁帮紝瀵逛簬姣忎釜绾︽暟x锛屾樉鐒? gcd(i/x,n/x)=1$
鎵€浠ユ垜浠绠椾竴涓媙/x鐨勬鎷夊嚱鏁板氨ok浜嗐€?/p>
鑱旇禌鍓嶅埛姘撮qwq
浠g爜
// #include <bits/stdc++.h> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 7000; ll a[maxn], top=0, p[maxn]; int fac(ll x) { int n=x, flag=0; ll end = sqrt(x); for(ll i=2;i<=end;i++) { if(x%i==0) { flag=1; a[++top] = i; if(i*i!=x) a[++top] = x/i; while(n%i==0) n/=i; } } if(x==2) flag=0; return flag; } ll phi(ll x) { ll ans = x, end = sqrt(x)+1; for(ll i=2;i<=end;i++) { if(x%i==0) { ans = ans / i * (i-1); while(x%i==0) x/=i; } } if(x>1) ans = ans / x * (x-1); return ans; } void Ph() { for(int i=2;i<maxn-10;i++) p[i]=i; for(ll i=2;i<maxn-10;i++) { if(p[i]==i) { for(ll j=i;j<maxn-10;j+=i) { p[j] = p[j] / i * (i-1); } } } } int main() { int n; Ph(); while(scanf("%lld",&n)==1) { if(n==1){ printf("1 "); continue; } top=0; int q=fac(n); if(q == 0) { printf("%lld ",phi(n)+n); continue; } sort(a+1,a+1+top); int pp = top; ll ans=phi(n)+n; for(int i=1;i<=pp;i++) { ll qwq; if(n/a[i] < maxn-10) qwq = p[n/a[i]]; else qwq = phi(n/a[i]); ans += a[i] * qwq; } printf("%lld ",ans); } }
以上是关于銆怭OJ銆?480 Longge's problem锛堟鎷夊嚱鏁帮級的主要内容,如果未能解决你的问题,请参考以下文章
题解报告:poj 2480 Longge's problem(欧拉函数)