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); }}
- 2016ICPC网赛青岛站1001 I Count Two Three丑数的应用求解
- 2016 ACM/ICPC 青岛区域赛网络赛 1001 I Count Two Three(打表+二分)
- hdu 5878 I Count Two Three ICPC青岛站网络赛1001
- hdu 5878 I Count Two Three 2016ACM/ICPC青岛赛区网络赛1001
- hdu5878 I Count Two Three -ICPC网络赛青岛赛区
- 青岛网赛1001 HDU5878 I Count Two Three
- HDU 5878 I Count Two Three 2016青岛网赛
- 2016 ACM/ICPC 青岛赛区网络赛 I Count Two Three
- HDU 5878 I Count Two Three(2016亚洲区青岛站网络赛)
- HDOJ 5878 I Count Two Three 【2016青岛网赛】暴力打表+二分枚举
- hdu 5878 I Count Two Three 丑数
- hdu 5878 I Count Two Three 丑数 二分
- HDU 5878 I Count Two Three 青岛网络赛
- 2016青岛online——I count two three
- HDU 5878 I Count Two Three 2016 ACM/ICPC Asia Regional Qingdao Online 1001
- HDU 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)
- hdu--5878(hdu 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001))
- I Count Two Three
- 1001 I Count Two Three(打表+二分 用lower_bound函数)
- [知识点滴]HTML5语义化总结
- iOS不用上架就能下载安装ipa应用内测:使用FIR.im发布自己的移动端APP
- java 适配器
- Android RxJava第三弹之RxJava2.0尝鲜
- 2016ICPC网赛青岛站1001 I Count Two Three丑数的应用求解
- JAVA的一次编译,到处运行,你知道多少?
- JAVA 概述!
- C++面试常见问题整理(待补充)
- hdu1004 (map总结)
- Redis应用——使用消息队列发布微博
- Emmet语法
- 为sys/cat文件生成测试签名
- Android socket聊天室