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;
}

  

时间: 2024-10-17 10:53:14

PKU Online Judge 1046 Square Number的相关文章

1046:Square Number

总时间限制: 1000ms 内存限制: 65536kB 描述 给定正整数b,求最大的整数a,满足a*(a+b) 为完全平方数 输入 多组数据,第一行T,表示数据数.对于每组数据,一行一个正整数表示b.T <= 10^3, 1 <= b <= 10^9 输出 对于每组数据,输出最大的整数a,满足a*(a+b)为完全平方数 样例输入 3 1 3 6 样例输出 0 1 2思路:a*(a+b):gcd(a,b) = gcd(a,a+b),这个符合辗转相除,然后a*(a+b)=gcd^2(a1)*

山东省第六届“浪潮杯”ACM程序设计大赛:D:Square Number

Description: In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinc

山东省第六届省赛 H题:Square Number

Description In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct

【第七届山东省ACM竞赛】Square Number

思路比较明确,就是一个数,如果和另外一个数乘起来是个平方数的话,那么满足一个条件 数A可以分解成为n1 个 a1,n2 个 a2 -- 数B可以分解成为m1个 a1,m2 个 a2-- 这满足的条件是(ni + mi) % 2 == 0 一个数的分解出来奇个数的因子乘起来得到的值为v,找之前有几个数他的奇个数因子成积为v 代码如下: #include<cmath> #include<cstdio> #include<cstring> #include<iostre

SDUT 3258 Square Number 简单数学

和上一题一样,把平方因子除去,然后对应的数就变成固定的 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=1e6+5; const int INF=0x3f3f3f3f; int vis[N],prime[1005],cnt; void getp

UVA 11461 Square Numbers解题报告

Discription A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). In

山东省第六届ACM省赛 H---Square Number 【思考】

题目描述 In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct intege

钥匙计数之一(递推)hdu1438

钥匙计数之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1471    Accepted Submission(s): 622 Problem Description 一把锁匙有N个槽,槽深为1,2,3,4.每锁匙至少有3个不同的深度且至少有1对相连的槽其深度之差为3.求这样的锁匙的总数. Input 本题无输入 Output 对

HDU-1438 钥匙计数之一

http://acm.hdu.edu.cn/showproblem.php?pid=1438                                钥匙计数之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1328    Accepted Submission(s): 552 Problem Description 一把锁匙有