1. 程式人生 > >ZCMU-2014: 一生之敵(數學+列舉)

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