求N(10^14)以內與N互質的數的和(容斥原理,或者尤拉函式)
阿新 • • 發佈:2018-12-24
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <cstdio> #include <map> #include <queue> #include <stack> #include <vector> #include <set> #define LL long long #define INF 0x3f3f3f3f using namespace std; const int mod=1000000007; const int maxn=205; const int inv=500000004; LL n; LL ms(LL x) { x %= mod; return (x * (x + 1) % mod) * inv % mod; } void solve() { LL t=n; vector<LL>v; for(int i=2;i<=sqrt(n);i++) { if(t%i==0) { v.push_back(i); while(t%i==0) { t/=i; } } } if(t>1) v.push_back(t); int len=v.size(); LL ans=0; for(int i=1;i<(1<<len);i++) { int cnt=0; LL temp=1; for(int j=0;j<len;j++) { if((i>>j)&1) { cnt++; temp*=v[j]; } } LL num=n/temp; if(cnt&1) { ans = (ans + (temp%mod) * ms(num) % mod) % mod; } else { ans=(ans- (temp%mod) * ms(num) % mod+mod)%mod; } } cout << ans << endl; } int main() { while(cin>>n) { solve(); } return 0; }
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <cstdio> #include <map> #include <queue> #include <stack> #include <vector> #include <set> #define INF 0x3f3f3f3f #define LL long long using namespace std; const int mod=1e9+7; const int maxn=1e5+5; const int inv=5e8+4; LL n; LL phi(LL n) { LL ans=n; for(LL i=2;i<=sqrt(n);i++) { if(n%i==0) { ans=ans/i*(i-1); while(n%i==0) { n/=i; } } } if(n>1) ans=ans/n*(n-1); return ans; } int main() { ios::sync_with_stdio(false); //cout << phi(100000000000000) << endl; while(cin>>n) { LL t=n; t%=mod; LL ans=((t*(t+1)%mod-(t*(phi(n)%mod))%mod+mod)%mod)*inv; ans%=mod; cout << ans << endl; } return 0; }