2018沈阳网络赛 Convex Hull(莫比乌斯,容斥)
#include<bits/stdc++.h> #define me(a,x) memset(a,x,sizeof(a)) #define sc scanf #define itn int #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); #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=1e6; const long long mod=1e9+7; 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 x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;} ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;} void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=(a/b)*x;} ll n,p; ll pow_2(ll x){ ll ans=mul(x,x+1,6LL*p); ans=mul(ans,2LL*x+1,6LL*p); ans/=6; return ans; } ll pow_3(ll x){ ll ans=mul(x,x+1,2LL*p); ans/=2; ans=mul(ans,ans,p); return ans; } int prime[N],tot=0; bool vis[N]; short int mu[N]; void pre(){ mu[1]=1; for(int i=2;i<N;i++){ if(!vis[i])prime[++tot]=i,mu[i]=-1; 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; break; }else mu[i*prime[j]]=-mu[i]; } } } int main(){ pre(); while(sc("%lld%lld",&n,&p)>0){ ll ans=0,res=0; ll T=sqrt(n+0.5); for(ll i=1;i<=T;i++){ ll x=mul(i*i,i*i,p); ans+=mu[i]*mul(pow_2(n/(i*i)),x,p); if(ans>=p)ans-=ans/p*p; if(ans<0)ans=(ans%p+p)%p; x=mul(x,i*i,p); res+=mu[i]*mul(pow_3(n/(i*i)),x,p); if(res>=p)res-=res/p*p; if(res<0)res=(res%p+p)%p; } printf("%lld\n",(mul(n+1,ans,p)-res+p)%p); } }