ZCMU-2014: 一生之敵(數學+枚舉)

2014: 一生之敵

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

Description

1(11).pnguploading.4e448015.gif轉存失敗重新上傳取消

 

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+1)*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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章