洛谷P1587 [NOI2016]循環之美
阿新 • • 發佈:2018-09-25
else target pre log div style -i min ans
傳送門
不會,先坑著
https://kelin.blog.luogu.org/solution-p1587
1 //minamoto 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int N=7e5+5,E=2e6+5; 7 typedef int arr[N];typedef long long ll; 8 struct Am{int nx,x,w;}e1[E]; 9 struct Ans{intnx,n,m,k;ll w;}e2[E]; 10 int n,m,k,M,c1,c2,K[2005],f1[E],f2[E];arr vis,p,mu,sum; 11 int Sm(int x){ 12 if(x<=M) return sum[x]; 13 int u=(x+2017)%E; 14 for(int i=f1[u];i;i=e1[i].nx) 15 if(e1[i].x==x) return e1[i].w; 16 e1[++c1]=(Am){f1[u],x,1},f1[u]=c1; 17 int &w=e1[c1].w,i=2,j=sqrt(x); 18 for(;i<=j;++i) w-=Sm(x/i); 19 for(;i<=x;i=j+1) 20 j=x/(x/i),w-=(j-i+1)*Sm(x/i); 21 return w; 22 } 23 ll sol(int n,int m,int k){ 24 if(!n||!m) return 0; 25 int u=(2017ll*n+m+k)%E; 26 for(int i=f2[u];i;i=e2[i].nx) 27 if(e2[i].n==n&&e2[i].m==m&&e2[i].k==k) returne2[i].w; 28 e2[++c2]=(Ans){f2[u],n,m,k,0},f2[u]=c2;ll &w=e2[c2].w; 29 if(k==1){ 30 if(n>m) swap(n,m); 31 int i=1,j=sqrt(n),s,t=0,x,y; 32 for(;i<=j;++i,t=s) s=Sm(i),w+=1ll*(n/i)*(m/i)*(s-t); 33 for(;i<=n;i=j+1,t=s)x=n/i,y=m/i,j=min(n/x,m/y),s=Sm(j),w+=1ll*x*y*(s-t); 34 u=(2017ll*m+n+k)%E;e2[++c2]=(Ans){f2[u],m,n,k,w},f2[u]=c2; 35 } 36 else for(int i=1;i<=K[0]&&K[i]<=k;++i) 37 if(k%K[i]==0&&mu[K[i]]) 38 w+=sol(m/K[i],n,K[i])*mu[K[i]]; 39 return w; 40 } 41 int main(){ 42 // freopen("testdata.in","r",stdin); 43 scanf("%d%d%d",&n,&m,&k); 44 M=min(N-5,max(k,min(n,m))),sum[1]=mu[1]=1; 45 for(int i=2;i<=M;++i){ 46 if(!vis[i]) p[++p[0]]=i,mu[i]=-1; 47 for(int j=1,x;j<=p[0]&&(x=i*p[j])<=M;++j){ 48 vis[x]=1; 49 if(i%p[j]==0) break; 50 mu[x]=-mu[i]; 51 } 52 sum[i]=sum[i-1]+mu[i]; 53 } 54 for(int i=1;i<=k;++i) 55 if(k%i==0) K[++K[0]]=i; 56 printf("%lld\n",sol(n,m,k)); 57 return 0; 58 }
洛谷P1587 [NOI2016]循環之美