[51nod1659]数方块
题目链接:
简单数学题。
假设\(n\le m\),那么枚举正方形边长\(1\le i\le n\),有:
\(F(n,m)=\sum_{i=1}^n\limits (n-i+1)(m-i+1)\)
\(=\sum_{i=1}^n\limits nm-\sum_{i=1}^n\limits ni+\sum_{i=1}^n\limits n-\sum_{i=1}^n\limits im+\sum_{i=1}^n\limits i^2-\sum_{i=1}^n\limits i+\sum_{i=1}^n\limits m-\sum_{i=1}^n\limits i+\sum_{i=1}^n\limits 1\)
\(=n^2m-n\frac{n(n+1)}{2}+n^2-m\frac{n(n+1)}{2}+\frac{n(n+1)(2n+1)}{6}-\frac{n(n+1)}{2}+nm-\frac{n(n+1)}{2}+n\)
\(=\frac{3n^2m+3nm-n^3+n}{6}\)
那么枚举\(n(F(n,n)\le x)\),求出\(m\)即可。
时间复杂度 \(O(\sqrt[3]{x})\)
代码:
#include <cstdio>
typedef long long ll;
typedef unsigned long long ull;
ull F(const ull n,const ull m)
{return (3*n*n*m+3*n*m-n*n*n+n)/6;}
int s,an;
ull x,a1[1000005],a2[1000005];
int main()
{
scanf("%llu",&x);
for(ull n=1;F(n,n)<=x;++n)
{
if((6*x+n*n*n-n)%(3*n*n+3*n))continue;
ull m=(6*x+n*n*n-n)/(3*n*n+3*n);
a1[++an]=n,a2[an]=m,s+=1+(n!=m);
}
printf("%d\n",s);
for(int i=1;i<=an;++i)printf("%llu %llu\n",a1[i],a2[i]);
for(int i=an-(s&1);i>=1;--i)printf("%llu %llu\n",a2[i],a1[i]);
return 0;
}