• 周三. 12 月 11th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

[51nod1659]数方块

admin

11 月 28, 2021

题目链接:

51nod1659

简单数学题。

假设(nle m),那么枚举正方形边长(1le ile n),有:

(F(n,m)=sum_{i=1}^nlimits (n-i+1)(m-i+1))

(=sum_{i=1}^nlimits nm-sum_{i=1}^nlimits ni+sum_{i=1}^nlimits n-sum_{i=1}^nlimits im+sum_{i=1}^nlimits i^2-sum_{i=1}^nlimits i+sum_{i=1}^nlimits m-sum_{i=1}^nlimits i+sum_{i=1}^nlimits 1)

(=n^2m-nfrac{n(n+1)}{2}+n^2-mfrac{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
",s);
	for(int i=1;i<=an;++i)printf("%llu %llu
",a1[i],a2[i]);
	for(int i=an-(s&1);i>=1;--i)printf("%llu %llu
",a2[i],a1[i]);
	return 0;
}

发表回复