2019南昌网络邀请赛 G(莫比乌斯反演)
#include<bits/stdc++.h> #define me(a,x) memset(a,x,sizeof(a)) #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); #define sc scanf #define itn int #define STR clock_t startTime = clock(); #define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl; using namespace std; const int N=1e7+5; const long long mod=1LL<<30; const long long mod2=998244353; const int oo=0x7fffffff; const int sup=0x80000000; typedef long long ll; typedef unsigned long long ull; template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");} template <typename it> string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;} template <typename it>int o(it a){cout<<a<<endl;return 0;} inline ll mul(ll a,ll b,ll c){ll ans=0;for(;b;b>>=1,a=(a+a)%c)if(b&1)ans=(ans+a)%c;return ans;} inline ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=mul(a,a,c))if(b&1)ans=mul(ans,a,c);return ans;} inline void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=x*(a/b);} ll inv_1(ll x){return x;} ll inv_2(ll x){return x*(x+1)%(2LL*mod)/2;} ll inv_6(ll x){return x*(x+1)%(6LL*mod)*(2LL*x+1)%(6LL*mod)/6;} int phi[N],prime[N],tot=0; short int mu[N]; bool vis[N]={0}; ll F[N]; void f_mod(ll &x){ if(x>=mod)x-=x/mod*mod; if(x<0)x+=mod; } void pre(){ F[1]=mu[1]=phi[1]=vis[1]=1; for(int i=2;i<N;i++){ if(!vis[i])prime[++tot]=i,phi[i]=i-1,mu[i]=-1,F[i]=i-2; for(int j=1;j<=tot&&i*prime[j]<N;j++){ vis[i*prime[j]]=1; if(i%prime[j]==0){ mu[i*prime[j]]=0; phi[i*prime[j]]=phi[i]*prime[j]; if(i==prime[j])F[i*prime[j]]=1LL*(prime[j]-1)*(prime[j]-1)%mod; else if(i%(1LL*prime[j]*prime[j])==0)F[i*prime[j]]=F[i]*prime[j]%mod; else F[i*prime[j]]=F[i/prime[j]]*(1LL*(prime[j]-1)*(prime[j]-1)%mod)%mod; break; }else mu[i*prime[j]]=-mu[i],phi[i*prime[j]]=phi[i]*(prime[j]-1),F[i*prime[j]]=F[i]*F[prime[j]]%mod; } } for(int i=1;i<N;i++)F[i]=(1LL*i*i%mod*i%mod*F[i]%mod+F[i-1])%mod; } int main(){ // STR pre(); int t;cin>>t; while(t--){ int n;sc("%d",&n); ll ans=0; for(int i=1,last;i<=n;i=last+1){ last=n/(n/i); ll x=inv_1(n/i)*inv_2(n/i); f_mod(x); x*=inv_6(n/i); f_mod(x); x*=F[last]-F[i-1]+mod; f_mod(x); ans+=x; f_mod(ans); } printf("%lld\n",ans); } // END }