首页 > 代码库 > PKU Online Judge 1046 Square Number

PKU Online Judge 1046 Square Number

一道类似高中数学竞赛的构造题目,我们应该是把a写成b的某种形式然后得出答案,唉~

思路来自http://www.voidcn.com/blog/u013614281/article/p-2701125.html~

a*a + ab = (a+t)*(a+t)

a*b = 2*a*t+t*t

a = t*t/(b-2t)

如果b是奇数,那么t = (b-1)/2

如果b是偶数,如果(b-2)/2是偶数,t = (b-2)/2

如果b是偶数,而且(b-2)/2是奇数,那么t = (b - 4)/2

代码如下:

#include <stdio.h>
typedef long long ll;

int main()
{
	ll b;
	int t1;
	scanf("%d", &t1);
	while(t1--)
	{
		long long t;
		scanf("%lld", &b);
		if(b % 2 == 1)
		{
			t = (b - 1) / 2;
			printf("%lld\n", t * t);
		}

		else if(b % 2 == 0 && ( (b - 2) / 2) % 2 ==0)
		{
			t = (b - 2) / 2;
			printf("%lld\n", t * t / 2);
		}

		else
		{
			t = (b - 4) / 2;
			printf("%lld\n", t * t / 4);

		}

	}
	return 0;
}

  

PKU Online Judge 1046 Square Number