码迷,mamicode.com
首页 > 其他好文 > 详细

2016青岛网络赛 I Count Two Three(打表+二分)

时间:2018-08-14 21:07:13      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:nts   color   com   ini   output   -o   blank   inf   data   

I Count Two Three 

  •  31.1%
  •  1000ms
  •  32768K
 

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 2^a3^b5^c7^d2a3b5c7d which a,\ b,\ c,\ da, b, c, d are non-negative integers.

At recent dinner parties, we call the integers with the form 2^a3^b5^c7^d2a3b5c7d "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 Format

The first line of input contains an integer t (1\le t \le 500000)t(1t500000), the number of test cases. tt test cases follow. Each test case provides one integer n (1\le n\le 10^9)n(1n109).

Output Format

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

样例输入

10
1
11
13
123
1234
12345
123456
1234567
12345678
123456789

样例输出

1
12
14
125
1250
12348
123480
1234800
12348000
123480000

题目来源

ACM-ICPC 2016 Qingdao Preliminary Contest

 

 

这道题类似于之前的51Nod - 1284找2 3 5 7的倍数,求出2 3 5 7最小公倍数210找循环节。

本题是2^a*3^b*5^c*7^d,无法从gcd循环节入手,于是就把10^9之内满足条件的值打出表来,发现只有5194个。

由于t很大,将表排个序,二分查找即可。

 

#include<stdio.h>
#include<algorithm>
#define MAX 5200
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll a[MAX];
ll mul(ll init,int x,int c)
{
    ll ans=init;
    for(int i=1;i<=c;i++){
        ans*=x;
        if(ans>1000000000) return 0;
    }
    return ans;
}
int main()
{
    int t,i,j,k,l;
    int tt=0;
    for(i=0;i<=31;i++){
        ll ii=mul(1,2,i);
        if(ii==0) continue;
        for(j=0;j<=31;j++){
            ll jj=mul(ii,3,j);
            if(jj==0) continue;
            for(k=0;k<=31;k++){
                ll kk=mul(jj,5,k);
                if(kk==0) continue;
                for(l=0;l<=31;l++){
                    ll lll=mul(kk,7,l);
                    if(lll==0) continue;
                    a[++tt]=lll;
                }
            }
        }
    }
    sort(a+1,a+tt+1);
    ll n;
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&n);
        ll ans=INF;
        int l=1,r=tt,m;
        while(l<=r){
            m=(l+r)/2;
            if(a[m]==n){
                ans=n;
                break;
            }
            if(a[m]<n){
                l=m+1;
            }
            else{
                if(a[m]<ans) ans=a[m];
                r=m-1;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

2016青岛网络赛 I Count Two Three(打表+二分)

标签:nts   color   com   ini   output   -o   blank   inf   data   

原文地址:https://www.cnblogs.com/yzm10/p/9477438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!