I Count Two Three
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 688 Accepted Submission(s): 360
Problem Description
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 i -th attacks failed if and only if i can be rewritten as the form of 2a3b5c7d which a,b,c,d are non-negative integers.
At recent dinner parties, we call the integers with the form 2a3b5c7d "I Count Two Three Numbers".
A related board game with a given positive integer n from one agent, asks all participants the smallest "I Count Two Three Number" no smaller than n .
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 i -th attacks failed if and only if i can be rewritten as the form of 2a3b5c7d which a,b,c,d are non-negative integers.
At recent dinner parties, we call the integers with the form 2a3b5c7d "I Count Two Three Numbers".
A related board game with a given positive integer n from one agent, asks all participants the smallest "I Count Two Three Number" no smaller than n .
Input
The first line of input contains an integer
t (1≤t≤500000)
, the number of test cases.
t
test cases follow. Each test case provides one integer
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
n
.
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
Source
题意:找到由2a3b5c7d组成的第一个大于等于n的值
分析:由2a3b5c7d 组成的值很少,只有几千,预处理打表,然后用二分查找
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
#define ll long long
ll a[10005];
ll mypow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a);
b>>=1;
a=a*a;
}
return ans;
}
int main()
{
int t,x,f1,f2,f3;
int cnt=0;
for(int i=0; i<30; i++)
{
f1=0;
for(int j=0; j<30; j++)
{
f2=0;
for(int k=0; k<30; k++)
{
f3=0;
for(int l=0; l<30; l++)
{
ll sum=1;
sum*=mypow(2,i);
if(sum>1e9)
{
f1=1;
break;
}
sum*=mypow(3,j);
if(sum>1e9)
{
f2=1;
break;
}
sum*=mypow(5,k);
if(sum>1e9)
{
f3=1;
break;
}
sum*=mypow(7,l);
if(sum>1e9)
{
break;
}
a[cnt++]=sum;
}
if(f3==1)
break;
}
if(f2==1)
break;
}
if(f1==1)
break;
}
sort(a,a+cnt);
scanf("%d",&t);
while(t--)
{
scanf("%d",&x);
int p=lower_bound(a,a+cnt,x)-a;
printf("%lld\n",a[p]);
}
return 0;
}