2016 ACM/ICPC 青島網賽 1001(預處理+二分)

I Count Two Three


題意:求能用2^a*3^b*5^c*7^d表示的大於等於n的值,其中a,b,c,d均爲非負整數。


我們可以先把能用2^a*3^b*5^c*7^d表示的數打表,,然後二分即可。n的最大值爲10^9,估算一下數組開到6000即可。


代碼:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int max_n=6000;
const int inf=1000000000;

int b[max_n]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int p[4]={2, 3, 5, 7};
int n; 

void init()
{
    int crt;
    for (int i=10;i<max_n;i++)
    {
        crt=i-1;
        b[i]=inf;
        for (int j=0;j<4;j++)
        {
            while(b[crt-1]*p[j]>b[i-1])   crt--;
            b[i]=min(b[i],b[crt]*p[j]);
        }
    }
}

int main()
{
    init();

    int t;
    cin >> t;

    while (t--)
    {
        scanf("%d",&n);
        int res=*lower_bound(b,b+max_n,n);
        printf("%d\n",res);
    }       
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章