2016ICPC网赛青岛站1001 I Count Two Three丑数的应用求解

来源:互联网 发布:淘宝定向推广的条件 编辑:程序博客网 时间:2024/05/13 16:03

题目源:> 引用块内容

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
12345678
123456789

Sample Output

1
12
14
125
1250
12348
123480
1234800
12348000
123480000

简单来说题目要去我们输出大于或等于所给n的最小的丑数,只是这里的丑数定义为仅包含2、3、5、7四个质数的合数。采用一遍扫描自然是超时的,这里采用打表的方法解决。一般认为1是最小的丑数,接下来就可以依次生成。可以发现从小到大生成丑数时,添在队尾的丑数应该是前面丑数中分别乘以2、3、5、7刚好大于目前最大丑数的4个丑数中最小的一个。ac代码如下。

这里写代码片#include<bits/stdc++.h>using namespace std;int min4(int a, int b, int c, int d){    int tmp = (a < b ? a : b);    tmp = (tmp < c ? tmp : c);    return (tmp < d ? tmp : d);}int main(){    vector<int> uglyNum;    uglyNum.push_back(1);  //初始只有1在数组中    int t2 = 0, t3 = 0, t5 = 0, t7 = 0; //每个因子开始相乘的下标位置    int i, j, k, l;    while (uglyNum.size() != 5200)    {        int m2, m3, m5, m7; //保存每个因子得到的值        for (i = t2; i < uglyNum.size(); i++)            if (2 * uglyNum[i] > uglyNum.back())            {                m2 = 2 * uglyNum[i];                break;            }        for (j = t3; j < uglyNum.size(); j++)            if (3 * uglyNum[j] > uglyNum.back())            {                m3 = 3 * uglyNum[j];                break;            }        for (k = t5; k < uglyNum.size(); k++)            if (5 * uglyNum[k] > uglyNum.back())            {                m5 = 5 * uglyNum[k];                break;            }        for (l = t7; l < uglyNum.size(); l++)            if (7 * uglyNum[l] > uglyNum.back())            {                m7 = 7 * uglyNum[l];                break;            }        int tmp = min4(m2, m3, m5, m7);        uglyNum.push_back(tmp);        if (tmp == m2)            t2 = i + 1; //下标位置进行更新        if (tmp == m3)            t3 = j + 1;        if (tmp == m5)            t5 = k + 1;        if (tmp == m7)            t7 = l + 1;    }    int t;    scanf("%d", &t);    while (t--)    {        int n;        scanf("%d", &n);        int ans = *lower_bound(uglyNum.begin(), uglyNum.end(), n);        printf("%d\n", ans);    }}
0 0
原创粉丝点击