标签:
上次看莫比乌斯繁衍反演是一个月前,讲道理没怎么看懂..
//HAOI 2011 B //by Cydiater //2016.7.25 #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <cstdio> #include <cstdlib> #include <ctime> #include <cmath> #include <iomanip> #include <string> #include <queue> #include <map> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) #define FILE "b" const int MAXN=5e5+5; const int oo=0x3f3f3f3f; inline int read(){ char ch=getchar();int x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int prime[MAXN],mu[MAXN],cnt=0,T,a,b,c,d,sum[MAXN],k; bool vis[MAXN]; namespace solution{ void make_mu(){ mu[1]=1; memset(vis,0,sizeof(vis)); up(i,2,50000){ if(!vis[i]){prime[++cnt]=i;mu[i]=-1;} for(int j=1;prime[j]*i<=50000&&j<=cnt;j++){ vis[prime[j]*i]=1; if(i%prime[j]!=0)mu[i*prime[j]]=-mu[i]; else{ mu[i*prime[j]]=0; break; } } } up(i,1,50000)sum[i]=sum[i-1]+mu[i]; } int get(int n,int m){ if(n>m)swap(n,m); int pos,ans=0; for(int i=1;i<=n;i=pos+1){ pos=min(n/(n/i),m/(m/i)); ans+=(sum[pos]-sum[i-1])*(n/i)*(m/i); } return ans; } } int main(){ //freopen("input.in","r",stdin); freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); T=read(); using namespace solution; memset(sum,0,sizeof(sum)); make_mu(); while(T--){ a=read();b=read();c=read();d=read();k=read(); a--;c--; a/=k;b/=k;c/=k;d/=k; printf("%d\n",get(b,d)-get(a,d)-get(b,c)+get(a,c)); } return 0; }
BZOJ 1101
//BZOJ 1101 //by Cydiater //2016.7.25 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <ctime> #include <cmath> #include <cstdlib> #include <queue> #include <map> #include <iomanip> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) const int MAXN=5e4+50; const int oo=0x3f3f3f3f; inline int read(){ char ch=getchar();int x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int mu[MAXN],prime[MAXN],cnt=0,sum[MAXN],T; bool vis[MAXN]; namespace solution{ void make_mu(){ memset(mu,0,sizeof(mu)); memset(sum,0,sizeof(sum)); mu[1]=1; up(i,2,50000){ if(!vis[i]){prime[++cnt]=i;mu[i]=-1;} for(int j=1;j<=cnt&&i*prime[j]<=50000;j++){ vis[i*prime[j]]=1; if(i%prime[j]!=0)mu[i*prime[j]]=-mu[i]; else{ mu[i*prime[j]]=0; break; } } } up(i,1,50000)sum[i]=sum[i-1]+mu[i]; } int get(int n,int m){ int pos,ans=0; if(n>m)swap(n,m); for(int i=1;i<=n;i=pos+1){ pos=min(m/(m/i),n/(n/i)); ans+=(sum[pos]-sum[i-1])*(m/i)*(n/i); } return ans; } } int main(){ //freopen("input.in","r",stdin); using namespace solution; make_mu(); T=read(); while(T--){ int a=read(),b=read(),k=read(); a/=k;b/=k; printf("%d\n",get(a,b)); } return 0; }
//Vijos 1889 //by Cydiater //2016.7.25 #include <iostream> #include <iomanip> #include <cstdio> #include <cstring> #include <string> #include <ctime> #include <cmath> #include <queue> #include <map> #include <cstdlib> #include <algorithm> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) const int MAXN=200000+50; const int oo=0x3f3f3f3f; inline ll read(){ char ch=getchar();ll x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll N,prime[MAXN],cnt=0,mu[MAXN],leftt,rightt,mid; bool vis[MAXN]; namespace solution{ void make_mu(){ memset(vis,0,sizeof(vis)); memset(mu,0,sizeof(mu)); up(i,2,200000){ if(!vis[i]){prime[++cnt]=i;mu[i]=1;} for(int j=1;prime[j]*i<=200000&&j<=cnt;j++){ vis[prime[j]*i]=1; if(i%prime[j]!=0)mu[i*prime[j]]=-mu[i]; else{ mu[i*prime[j]]=0; break; } } } } void init(){ N=read(); } bool check(ll num){ ll lim=1LL*sqrt(1.0*num),ans=0; for(ll i=2;i<=lim;i++) ans+=num/(i*i)*mu[i]; return ans>=N; } void slove(){ leftt=N;rightt=25505460948LL; while(leftt+1<rightt){ mid=(leftt+rightt)>>1; if(check(mid)) rightt=mid; else leftt=mid; } if(check(leftt))cout<<leftt<<endl; else cout<<rightt<<endl; } } int main(){ //freopen("input.in","r",stdin); //freopen("out.out","w",stdout); using namespace solution; make_mu(); init(); slove(); return 0; }
BZOJ 2440
//bzoj2440 //by Cydiater //2016.7.25 #include <iostream> #include <iomanip> #include <cstdio> #include <cstring> #include <string> #include <ctime> #include <cmath> #include <queue> #include <map> #include <cstdlib> #include <algorithm> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) const int MAXN=200000+50; const int oo=0x3f3f3f3f; inline ll read(){ char ch=getchar();ll x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll N,prime[MAXN],cnt=0,mu[MAXN],leftt,rightt,mid,T; bool vis[MAXN]; namespace solution{ void make_mu(){ memset(vis,0,sizeof(vis)); memset(mu,0,sizeof(mu)); up(i,2,200000){ if(!vis[i]){prime[++cnt]=i;mu[i]=1;} for(int j=1;prime[j]*i<=200000&&j<=cnt;j++){ vis[prime[j]*i]=1; if(i%prime[j]!=0)mu[i*prime[j]]=-mu[i]; else{ mu[i*prime[j]]=0; break; } } } } void init(){ N=read(); } bool check(ll num){ ll lim=1LL*sqrt(1.0*num),ans=0; for(ll i=2;i<=lim;i++) ans+=num/(i*i)*mu[i]; return num-ans>=N; } void slove(){ leftt=N;rightt=25505460948LL; while(leftt+1<rightt){ mid=(leftt+rightt)>>1; if(check(mid)) rightt=mid; else leftt=mid; } if(check(leftt))cout<<leftt<<endl; else cout<<rightt<<endl; } } int main(){ //freopen("input.in","r",stdin); //freopen("out.out","w",stdout); using namespace solution; make_mu(); T=read(); while(T--){ init(); slove(); } return 0; }
YY的GCD
//YY de GCD //by Cydiater //2016.7.26 #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <cstdlib> #include <cstdio> #include <iomanip> #include <ctime> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) #define FILE "YYnoGCD" const int oo=0x3f3f3f3f; const int MAXN=1e7+5; inline ll read(){ char ch=getchar();ll x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int prime[MAXN],cnt=0,f[MAXN],T,mu[MAXN]; bool vis[MAXN]; namespace solution{ void make_mu(){ memset(vis,0,sizeof(vis)); memset(f,0,sizeof(f)); mu[1]=1; up(i,2,10000000){ if(!vis[i]){prime[++cnt]=i;mu[i]=-1;} up(j,1,cnt){ if(prime[j]*i>10000000)break; vis[prime[j]*i]=1; if(i%prime[j]!=0)mu[i*prime[j]]=-mu[i]; else{ mu[i*prime[j]]=0; break; } } } up(i,1,cnt)up(j,1,10000000){ if(prime[i]*j>10000000)break; f[prime[i]*j]+=mu[j]; } up(i,1,10000000)f[i]+=f[i-1]; } void slove(ll a,ll b){ ll ans=0,pos; if(a>b)swap(a,b); up(i,1,a){ pos=min(a/(a/i),b/(b/i)); ans+=(f[pos]-f[i-1])*(a/i)*(b/i); i=pos; } cout<<ans<<endl; } } int main(){ //freopen("input.in","r",stdin); freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); using namespace solution; make_mu(); T=read(); while(T--){ ll a=read(),b=read(); slove(a,b); } return 0; }
BZOJ 4407于神之怒
//BZOJ 4407 //by Cydiater //2016.7.27 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cstdlib> #include <queue> #include <map> #include <iomanip> #include <cmath> #include <ctime> using namespace std; #define ll long long #define up(i,j,n) for(int i=j;i<=n;i++) #define down(i,j,n) for(int i=j;i>=n;i--) const int MAXN=5e6+5; const int LIM=5e6; const int oo=0x3f3f3f3f; const int mod=1e9+7; inline int read(){ char ch=getchar();int x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll f[MAXN],prime[MAXN],cnt=0,N,K,g[MAXN],T; bool vis[MAXN]; namespace solution{ inline ll quick_pow(ll a,ll b){ ll tmp=1; while(b){ if(b&1)tmp=(tmp*a)%mod; a=(a*a)%mod; b>>=1; } return tmp; } void pret(){ memset(vis,0,sizeof(vis)); f[1]=1; up(i,2,LIM){ if(!vis[i]){prime[++cnt]=i;g[cnt]=quick_pow(i,K);f[i]=g[cnt]-1;} up(j,1,cnt){ if(i*prime[j]>LIM)break; vis[i*prime[j]]=1; if(i%prime[j]!=0){ f[i*prime[j]]=(f[i]*f[prime[j]])%mod; }else{ f[i*prime[j]]=(f[i]*g[j])%mod; break; } } } up(i,1,LIM)f[i]=(f[i-1]+f[i])%mod; } void slove(ll a,ll b){ ll ans=0,pos; if(a>b)swap(a,b); up(i,1,a){ pos=min(a/(a/i),b/(b/i)); ans+=(((f[pos]+mod-f[i-1])*(a/i))%mod)*(b/i); ans%=mod; i=pos; } printf("%lld\n",ans); } } int main(){ freopen("input.in","r",stdin); using namespace solution; T=read();K=read(); pret(); while(T--)slove(read(),read()); return 0; }
BZOJ 2154
然后双重分块就好了
//BZOJ 2154 //by Cydiater //2016.7.27 #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <queue> #include <map> #include <ctime> #include <cmath> #include <algorithm> #include <cstdlib> using namespace std; #define ll long long #define up(i,j,n) for(ll i=j;i<=n;i++) #define down(i,j,n) for(ll i=j;i>=n;i--) const ll MAXN=1e7+5; const ll LIM=1e7; const ll mod=20101009; const ll oo=0x3f3f3f3f; inline ll read(){ char ch=getchar();ll x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll prime[MAXN],cnt=0,mu[MAXN],N,M; bool vis[MAXN]; namespace solution{ void pret(){ memset(vis,0,sizeof(vis)); memset(mu,0,sizeof(mu)); mu[1]=1; up(i,2,N){ if(!vis[i]){prime[++cnt]=i;mu[i]=-1;} up(j,1,cnt){ if(prime[j]*i>N)break; vis[prime[j]*i]=1; if(i%prime[j]!=0){ mu[i*prime[j]]=-mu[i]; }else{ mu[i*prime[j]]=0; break; } } } up(i,1,N)mu[i]=(mu[i-1]+(mu[i]*i*i)%mod)%mod; } ll sum(ll a,ll b){ return ((a+1)*a/2%mod)*(b*(b+1)/2%mod)%mod; } ll F(ll a,ll b){ ll pos,ans=0; if(a>b)swap(a,b); up(i,1,a){ pos=min(a/(a/i),b/(b/i)); ans=(ans+(mu[pos]-mu[i-1])*sum(a/i,b/i))%mod; i=pos; } return ans; } void slove(ll a,ll b){ if(a>b)swap(a,b); ll pos,ans=0; up(i,1,a){ pos=min(a/(a/i),b/(b/i)); ans=(ans+(i+pos)*(pos-i+1)/2%mod*F(a/i,b/i)%mod)%mod; i=pos; } printf("%lld\n",(ans+mod)%mod); } } int main(){ freopen("input.in","r",stdin); using namespace solution; N=read();M=read(); if(N>M)swap(N,M); pret(); slove(N,M); //cout<<"Time has passed:"<<1.0*clock()/1000<<"s!"<<endl; return 0; }
BZOJ 2693:
//BZOJ 2693 //by Cydiater //2016.7.29 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <ctime> #include <queue> #include <map> #include <cstdlib> #include <iomanip> using namespace std; #define ll long long #define up(i,j,n) for(ll i=j;i<=n;i++) #define down(i,j,n) for(ll i=j;i>=n;i--) const int MAXN=1e7+10; const int LIM=1e7; const int mod=1e8+9; const int oo=0x3f3f3f3f; inline ll read(){ char ch=getchar();ll x=0,f=1; while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll f[MAXN],prime[MAXN],cnt=0,T; bool vis[MAXN]; namespace solution{ void pret(){ memset(vis,0,sizeof(vis)); f[1]=1; up(i,2,LIM){ if(!vis[i]){prime[++cnt]=i;f[i]=(i-i*i%mod)%mod;} up(j,1,cnt){ if(prime[j]*i>LIM)break; vis[prime[j]*i]=1; if(i%prime[j]==0){ f[i*prime[j]]=(f[i]*prime[j])%mod; break; } f[i*prime[j]]=(f[prime[j]]*f[i])%mod; } } up(i,1,LIM)f[i]=(f[i]+f[i-1])%mod; } ll sum(ll a,ll b){ ll tmp1=(a*(a+1)/2)%mod; ll tmp2=(b*(b+1)/2)%mod; return (tmp1*tmp2)%mod; } void slove(ll a,ll b){ if(a>b)swap(a,b); ll pos,ans=0; up(i,1,a){ pos=min(a/(a/i),b/(b/i)); ans=(ans+(f[pos]-f[i-1])*sum(a/i,b/i)%mod)%mod; i=pos; } printf("%lld\n",(ans+mod)%mod); } } int main(){ //freopen("input.in","r",stdin); using namespace solution; T=read(); pret(); while(T--)slove(read(),read()); return 0; }
标签:
原文地址:http://www.cnblogs.com/Cydiater/p/5793255.html