1. 程式人生 > >【51Nod - 1010】【只包含因子2 3 5的數】

【51Nod - 1010】【只包含因子2 3 5的數】

題目:

K的因子中只包含2 3 5。滿足條件的前10個數是:2,3,4,5,6,8,9,10,12,15。

所有這樣的K組成了一個序列S,現在給出一個數n,求S中 >= 給定數的最小的數。

例如:n = 13,S中 >= 13的最小的數是15,所以輸出15。

Input

第1行:一個數T,表示後面用作輸入測試的數的數量。(1 <= T <= 10000) 
第2 - T + 1行:每行1個數N(1 <= N <= 10^18)

Output

共T行,每行1個數,輸出>= n的最小的只包含因子2 3 5的數。

Sample Input

5
1
8
13
35
77

Sample Output

2
8
15
36
80

解題報告:

打表,直接迴圈暴力打出所有是2 3 5 倍數的數,然後sort一下,之後二分尋找合適的就可以。中間要防止數字爆long long。

ac程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1000105;
const long long int inf=1e18+100;
typedef long long ll;
ll num[maxn];
int cnt;
void db()
{
	cnt=0;
	for(ll i=1;i<inf;i*=2)
		for(ll j=1;i*j<inf;j*=3)
			for(ll k=1;i*j*k<inf;k*=5)
				num[cnt++]=i*j*k;
}
int main()
{
	db();
	sort(num,num+cnt);
	ll t,n;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&n);
		printf("%lld\n",*lower_bound(num+1,num+cnt,n));
	}
	return 0;
}