2016ICPC网赛青岛站1001 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
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值