BZOJ4115 : [Wf2015]Tile Cutting

#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 1048576
using namespace std;
typedef long long ll;
int n=500000,d[N],g[N],i,j,p[N],tot,T,l,r,ans;bool v[N>>1];ll f[N];
struct comp{
double r,i;comp(double _r=0,double _i=0){r=_r;i=_i;}
comp operator+(const comp x){return comp(r+x.r,i+x.i);}
comp operator-(const comp x){return comp(r-x.r,i-x.i);}
comp operator*(const comp x){return comp(r*x.r-i*x.i,r*x.i+i*x.r);}
}a[N];
const double pi=acos(-1.0);
void FFT(comp a[],int n,int t){
for(int i=1,j=0;i<n-1;i++){
for(int s=n;j^=s>>=1,~j&s;);
if(i<j)swap(a[i],a[j]);
}
for(int d=0;(1<<d)<n;d++){
int m=1<<d,m2=m<<1;
double o=pi/m*t;comp _w(cos(o),sin(o));
for(int i=0;i<n;i+=m2){
comp w(1,0);
for(int j=0;j<m;j++){
comp &A=a[i+j+m],&B=a[i+j],t=w*A;
A=B-t;B=B+t;w=w*_w;
}
}
}
if(t==-1)for(int i=0;i<n;i++)a[i].r/=n;
}
int main(){
for(d[1]=1,i=2;i<=n;i++){
if(!v[i])p[tot++]=i,d[i]=2,g[i]=1;
for(j=0;i*p[j]<=n&&j<tot;j++){
v[i*p[j]]=1;
if(i%p[j]){
d[i*p[j]]=d[i]*2;
g[i*p[j]]=1;
}else{
d[i*p[j]]=d[i]/(g[i]+1)*(g[i]+2);
g[i*p[j]]=g[i]+1;
break;
}
}
}
for(i=1;i<=n;i++)a[i].r=d[i];
FFT(a,N,1);
for(i=0;i<N;i++)a[i]=a[i]*a[i];
FFT(a,N,-1);
for(i=1;i<=n;i++)f[i]=(ll)(a[i].r+0.5);
for(scanf("%d",&T);T--;printf("%d %lld\n",ans,f[ans])){
scanf("%d%d",&l,&r);
for(ans=l;l<=r;l++)if(f[l]>f[ans])ans=l;
}
return 0;
}


posted @ 2015-09-03 00:47 Claris 阅读(...) 评论(...) 编辑 收藏