I Count Two Three(二分+打表)

I Count Two Three

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.

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

扫描二维码关注公众号,回复: 2867582 查看本文章

12345678

123456789

Sample Output

1

12

14

125

1250

12348

123480

1234800

12348000

123480000

题意:给出一个整数n, 找出一个大于等于n的最小整数m, 使得m可以表示为2^a * 3^b * 5^c * 7^d​​.

题解:打表预处理出所有满足要求的数,排个序然后二分查找解决。

 

AC代码:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=1e9+5;
int cnt=0;
ll str[500005];
void init()
{
    for(int i=0;i<=32;i++)
    {
        if(pow(2.0,i)>=INF)
            break;
        for(int j=0;j<=32;j++)
        {
            if(pow(3.0,j)*pow(2.0,i)>=INF)
                break;
            for(int k=0;k<=32;k++)
            {
                if(pow(5.0,k)*pow(3.0,j)*pow(2.0,i)>=INF)
                    break;
                for(int t=0;t<=32;t++)
                {
                    if(pow(7.0,t)*pow(5.0,k)*pow(3.0,j)*pow(2.0,i)>=INF)
                        break;
                    str[cnt++]=pow(7.0,t)*pow(5.0,k)*pow(3.0,j)*pow(2.0,i);
                }
            }
        }
    }
    sort(str,str+cnt);              //排序便于二分方程使用
}
int main()
{
    init();
    int n;
    cin>>n;
    while(n--)
    {
        ll m;
        scanf("%lld",&m);
        printf("%lld\n",str[lower_bound(str,str+cnt,m)-str]);   //二分查找方程,返回大于m的第一个值的位置
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/81676830