ZCMU-2014: 一生之敌(数学+枚举)

2014: 一生之敌

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 723  Solved: 116
[Submit][Status][Web Board]

Description

Input

 第一行输入一个整数T,表示数据组数。  
每组数据输入一个整数n。

 1 <= T <= 100000 
 0 <= n <= 10^19
保证结果存在 

Output

 输出一个整数。

Sample Input

3

2

6

100

Sample Output

6

6

114

HINT

Source

【解析】

数学题吧

由b^2 = 2*a*(a+1)^2得 b = (a+1)*根号下2a 

令i = 根号下2a,则a = i*i/2 , b = (i*i/2)*i,接下来暴力枚举一下i就可以了

然后二分查找(大于用lower_bound,大于等于用upper_bound)一下n就好了。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
vector<ull> ans;
int main()
{
	ull i = 0;
	while (1)
	{
		ull temp = i * i / 2;
		ans.push_back((temp + 1)*i);
		if (temp*(i + 1) > 1e19)break;
		i += 2;
	}
	int T;
	scanf("%d", &T);
	while (T--)
	{
		ull n;
		scanf("%llu", &n);
		int x = lower_bound(ans.begin(), ans.end(), n) - ans.begin();
		printf("%llu\n", ans[x]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/waterboy_cj/article/details/81318153