HDU 5878 I Count Two Three [ 打表 + 二分 ]

6 篇文章 0 订阅

题目链接:

2016 ACM/ICPC Asia Regional Qingdao Online HDU 5878 I Count Two Three

题意概括:

求可以写成 2^a3^b5^c7^d (a, b, c, d是非负整数),并且不大于 n 的最小整数。

数据范围:

1\leq t\leq500000 , 1\leq n\leq10^9

题解分析:

先预处理打表,求出所有可能的组合。在每次查询的时候,二分查找即可。

由于数据范围是 1\leq n\leq10^9 ,故上限的值为 2*10^{9} ,  打表时 dfs 到上限就可以停止。

并且根据  \left \lceil \log _22*10^{9} \right \rceil = 31,\left \lceil \log _32*10^{9}\right \rceil = 20,\left \lceil \log _52*10^{9} \right \rceil = 14,\left \lceil \log _72*10^{9} \right \rceil = 11 ,可以求出每层循环的次数。

二分用 lower_bound函数 实现即可。

AC代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 7e3;
typedef long long ll;
ll num[MAXN];

ll pow(int x, int n) {
    if (n == 0) return 1;
    ll res = pow(x * x, n / 2);
    if (n & 1) res = res * x;
    return res;
}

int main () {
    
    int cnt = 0;
    for (int i = 0; i < 11; i ++) {
        for (int j = 0; j < 14; j ++) {
            for (int i2 = 0; i2 < 20; i2 ++) {
                for (int j2 = 0; j2 < 31; j2 ++) {
                    ll temp = pow(7,i) * pow(5,j) * pow(3,i2) * pow(2,j2);
                    if (temp > 2e9) break;
                    else
                        num[cnt ++] = temp;
                }
            }
        }
    }

    int T;
    sort(num, num + cnt);
    scanf("%d", &T);
    while (T --) {
        int n;
        scanf("%d", &n);
        ll *ans = lower_bound(num, num + cnt, n);
        printf ("%lld\n", *ans);
    }
}

 

                                                     I Count Two Three

                       Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

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

Source

2016 ACM/ICPC Asia Regional Qingdao Online

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值