I Count Two Three
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1241 Accepted Submission(s): 583
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
题意:
·给一个数x,求数y使得y的因子只有2 3 5 7 ,并且y要大于x,求最小的满足题意的y;
解:
·根据题意直接采用打表的方式解决。其中问题放在解决求幂和查找上。此时采用快速幂的方法求幂。因为给定的数据数量太多,正常的查找会TLE,因此对数组进行排序之后采用二分查找。此处可以使用STL库中给出的*lower_bound(a,a+ge,n)来进行查找,也可以自己手敲二分查找。
使用*lower_bound(a,a+ge,n):
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
using namespace std;
long long a[100000];
long long pow (int x,int y)
{
x=(double)x;
y=(double)y;
long long ans=1;
while(y)
{
if(y&1)
ans*=x;
x*=x;
y>>=1;
}
return ans;
}
int t,n;
int main()
{
int ge=0;
for(int i=0; i<30; i++)
{
long long ii=pow(7,i);
for(int j=0; j<30; j++)
{
long long jj=pow(5,j);
for(int k=0; k<30; k++)
{
long long kk=pow(3,k);
for(int m=0; m<30; m++)
{
long long mm=pow(2,m);
if(ii*jj*kk*mm>7000000000)
break;
else
{
a[ge++]=ii*jj*kk*mm;
}
}
}
}
}
sort(a,a+ge);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%lld\n",*lower_bound(a,a+ge,n));
}
return 0;
}
手敲二分查找版:
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
using namespace std;
long long a[100000];
long long pow (int x,int y)
{
x=(double)x;
y=(double)y;
long long ans=1;
while(y)
{
if(y&1)
ans*=x;
x*=x;
y>>=1;
}
return ans;
}
int t,n;
int main()
{
int ge=0;
for(int i=0; i<30; i++)
{
long long ii=pow(7,i);
for(int j=0; j<30; j++)
{
long long jj=pow(5,j);
for(int k=0; k<30; k++)
{
long long kk=pow(3,k);
for(int m=0; m<30; m++)
{
long long mm=pow(2,m);
if(ii*jj*kk*mm>7000000000)
break;
else
{
a[ge++]=ii*jj*kk*mm;
}
}
}
}
}
sort(a,a+ge);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int low=0;
int high=ge-1;
int mid;
while(low<high)
{
mid=(low+high)/2;
if(a[mid]>=n)
high=mid;
if(a[mid]<n)
low=mid+1;
}
printf("%I64d\n",a[low]);
}
return 0;
}