1. 程式人生 > >I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)

I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)

I Count Two Three 

I will show you the most popular board game in the Shanghai Ingress Resistance Team. It all started several months ago.  We found out the home address of the enlightened agent Icount2three and decided to draw him out.  Millions of missiles were detonated, but some of them failed.  After the event, we analysed the laws of failed attacks.  It's interesting that the ii-th attacks failed if and only if ii can be rewritten as the form of 2a3b5c7d2a3b5c7d which a,b,c,da,b,c,d are non-negative integers.  At recent dinner parties, we call the integers with the form 2a3b5c7d2a3b5c7d "I Count Two Three Numbers".  A related board game with a given positive integer nn from one agent, asks all participants the smallest "I Count Two Three Number" no smaller than nn.

Input

The first line of input contains an integer t (1≤t≤500000)t (1≤t≤500000), the number of test cases. tt test cases follow. Each test case provides one integer n (1≤n≤109)n (1≤n≤109).

Output

For each test case, output one line with only one integer corresponding to the shortest "I Count Two Three Number" no smaller than nn.

Sample Input

10
1
11
13
123
1234
12345
123456
1234567
12345678
123456789

Sample Output

1
12
14
125
1250
12348
123480
1234800
12348000
123480000

AC程式碼

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

long long Ta[104200];
int tot=0;
void maketable(){
	tot=0;
	for(int i=0;i<=30;++i){
		for(int j=0;j<=19;j++){
			for(int n=0;n<=13;n++){
				for(int k=0;k<=11;++k){
					Ta[tot++]=pow(2,i)*pow(3,j)*pow(5,n)*pow(7,k);
				} 
			}  
		}
	}
} 

long long T,n;
int main(){
	cin>>T;
	maketable();
	sort(Ta,Ta+tot);
	while(T--){

		int ans=0;
		scanf("%lld",&n);
		printf("%lld\n", *lower_bound(Ta, Ta + tot, n));
		
	}
} 

第一次做這個題的時候暴力到崩了。總是T,第一次遇到打表這個名詞,不過一聽這個名詞就明白了,pow(2,a)*pow(3,b)*pow(5,c)*pow(7,d) 總共不過1e6數。所有結果列出來就二分了查找了。