2016 ACM/ICPC 青岛区域赛网络赛 1001 I Count Two Three(打表+二分)

I Count Two Three

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3582    Accepted Submission(s): 1094


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 (1t500000) , the number of test cases. t test cases follow. Each test case provides one integer n (1n109) .
 

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   题意: 给出一个数n,求一个数m,要求m为能够表示成m= 2a3b5c7d ,并且离n最近,比如n=11,距离11最近且能表示 成这种形式的数为12,,12=2^2*3^1*5^0*7^0,,  思路: 首先想到打表,找出1到1e9中满足条件的所有可能,,,但是求结果的时候要判断过程中如果数大于1e9, 则Break,,,结果一共有5000多个满足条件的数,,,最后求解的时候扫一遍就行了,,激动地敲完代码提交了一次 ,超时!!以为暴力的时候超时,各种改代码,,还是超时,,最后竟然死在求解那,,不能简单地扫一遍,,10^9 数量级,竟然没想到,,,最后看到题解,,用lower_bound()解决的。。 最后附上一条链接(<alogrithm>中常用函数)
alogrithm中常用函数

以下AC代码(参考)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
long long a[1000000];
long long pow(int x,int num){
    long long ans=1;
    for(long long i=1;i<=num;i++) ans=ans*x;
    return ans;
}
int main()
{
    int i,j,k,l;
    long long t,n;
    int index=0;
     for(i=0;i<=31;i++){
        for(j=0;j<=19;j++){
            for(k=0;k<=12;k++){
                for(l=0;l<=11;l++){
                    long long flag=pow(2,i);
                     if(flag>1e9) break;
                     flag*=pow(3,j);
                    if(flag>1e9) break;
                    flag*=pow(5,k);
                    if(flag>1e9) break;
                    flag*=pow(7,l);
                    if(flag>1e9) break;
                    a[index++]=flag;
            }
          }
        }
    }
            sort(a,a+index);
            scanf("%I64d",&t);
            while(t--)
            {
                int temp;
                scanf("%I64d",&n);
                temp=lower_bound(a,a+index,n)-a;
            printf("%I64d\n",a[temp]);
            }

    return 0;
}




   
   
   
   

你可能感兴趣的:(2016 ACM/ICPC 青岛区域赛网络赛 1001 I Count Two Three(打表+二分))