BZOJ3748 : [POI2015]Kwadraty
打表可得结论:
1.只有2,3,6,7,8,11,12,15,18,19,...,108,112,128这31个数的k值是无穷大
2.当n足够大的时候,即当n>506时,设$f(x)=1^2+2^2+...+x^2=\frac{x(x+1)(2x+1)}{6}$,
找到一个t使得$f(t-1)+1\leq n\leq f(t)$,
若k(f(t)-n)是无穷大,则k(n)=t+1,否则k(n)=t
所以当n<=506时,暴力打表,否则二分查找出这个t,然后套公式即可。
#include<cstdio> #define N 507 typedef long long ll; ll n,l=12,r=1442250,mid,t,ans; int i,j,v[N],sum[N],f[N]={0,1,0,0,2,2,0,0,0,3,3,0,0,3,3,0,4,4,0,0,4,4,0,0,0,4,4,0,0,4,4,0,0,0,5,5,6,6,5,5,6,5,5,0,0,5,5,0,0,6,5,5,6,6,5,5,6,6,7,7,0,6,6,7,8,6,6,0,8,7,6,6,0,8,6,6,0,6,6,7,8,6,6,7,7,7,6,6,7,7,6,6,0,8,7,7,0,9,7,7,7,7,7,7,7,7,7,9,0,8,7,7,0,8,7,7,8,8,8,7,7,8,8,7,7,8,7,7,0,8,7,7,9,8,8,7,7,9,8,7,7,8,8,8,9,8,8,8,8,8,8,8,8,8,8,8,9,10,8,8,9,9,8,8,8,8,8,8,8,8,8,9,9,10,8,8,9,10,8,8,9,9,9,8,8,9,9,8,8,10,8,8,9,10,8,8,9,9,9,8,8,9,9,8,8,9,9,9,9,10,9,9,9,10,9,9,9,9,10,9,9,9,9,9,9,10,9,9,9,9,9,9,9,9,9,9,9,10,10,9,9,10,10,9,9,9,9,9,9,9,9,9,10,10,10,9,9,11,10,9,9,10,10,10,9,9,10,10,9,9,10,9,9,11,10,9,9,11,10,10,9,9,10,10,9,9,10,10,10,11,10,10,10,11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,10,10,10,11,10,10,10,10,11,10,10,10,10,10,10,11,10,10,10,10,10,10,10,10,10,10,10,11,11,10,10,11,11,10,10,10,10,10,10,10,10,10,11,11,11,10,10,11,11,10,10,11,11,11,10,10,11,11,10,10,11,10,10,11,11,10,10,11,12,11,10,10,11,11,10,10,11,11,11,11,11,11,11,11,12,11,11,11,12,11,11,11,11,11,11,11,11,11,11,11,12,11,11,11,12,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,11,11,11,12,11,11,11,11,12,11,11,11,11,11,11,12,11,11,11,11,11,11,11,11,11,11,11,12,12,11,11,12,12,11,11,11,11,11,11,11,11,11,12,12,12,11,11,12,12,11,11,12,12,12,11,11,12,12,11,11,12,11,11,12,12,11,11,12,12,12,11,11,12,12,11,11}; ll F(ll x){return x*(x+1)*(x*2+1)/6;} int main(){ scanf("%lld",&n); for(i=2;i<N;i++)if(f[i])for(j=1;j<i;j++)if(!f[j]||f[j]>f[i])v[j]=1; for(i=2;i<N;i++)sum[i]=sum[i-1]+v[i]; if(n<N){ if(f[n])printf("%d",f[n]);else putchar('-'); return printf(" %d",sum[n]),0; } while(l<=r)if(F(mid=(l+r)>>1)>=n)r=(t=mid)-1;else l=mid+1; printf("%lld ",t+(F(t)>n&&F(t)-n<=128&&!f[F(t)-n])); for(ans=(t-12)*31+sum[N-1],i=1;i<=128;i++)if(!f[i]&&F(t)-i<=n)ans++; return printf("%lld",ans),0; }