51Nod-1010-只包含2 3 5的数
来源:互联网 发布:达达乐队知乎 编辑:程序博客网 时间:2024/06/06 02:35
ACM模版
描述
题解
先打表生成丑数,然后二分查找到大于等于n的第一个数。
代码
#include <iostream>#include <cstdio>#include <queue>using namespace std;typedef unsigned long long ull;const int MAXN = 1e4 + 1e3;/* * Ugly Numbers * Ugly numbers are numbers whose only prime factors are 2, 3 or 5. * 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... */typedef pair<ull, int> node_type;ull result[MAXN];void init(){ priority_queue<node_type, vector<node_type>, greater<node_type>> Q; Q.push(make_pair(1, 2)); for (int i = 0; i < MAXN; i++) { node_type node = Q.top(); Q.pop(); switch (node.second) { case 2: Q.push(make_pair(node.first * 2, 2)); case 3: Q.push(make_pair(node.first * 3, 3)); case 5: Q.push(make_pair(node.first * 5, 5)); } result[i] = node.first; } return ;}/* * 传入参数必须l <= h * 假设a数组已经按从小到大排序 * 返回值l总是合理的 */int bs(ull a[], int l, int h, ull v){ int m; while (l < h) { m = (l + h) >> 1; if (a[m] < v) { l = m + 1; } else { h = m; } } return l;}int main(int argc, const char * argv[]){ freopen("input.txt", "r", stdin);// freopen("input.txt", "w", stdin); init(); int T; cin >> T; ull n; while (T--) { cin >> n; int key = bs(result, 1, MAXN - 1, n); cout << result[key] << '\n'; } return 0;}
参考
STL简介
二分查找
0 0
- 51Nod-1010-只包含2 3 5的数
- 只包含因子2 3 5 的数----51Nod
- 51nod 1010 只包含因子2 3 5的数 (打表+二分)
- 51NOD 1010 只包含因子2 3 5的数(二分 + 预处理)
- 51nod 1010 只包含因子2 3 5的数
- 51nod:1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数(打表+二分)
- 51nod-1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod-1010 只包含因子2 3 5的数
- 51Nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod-1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数【打表+二分】
- 51Nod--1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 矩阵图求路径条数问题(简单dp)
- webstorm快捷键记录
- Spring框架中的单例Beans是线程安全的么?
- HDU 5780 gcd
- Scala开发教程(50): Ordered Trait
- 51Nod-1010-只包含2 3 5的数
- 筛法求质数,欧拉函数
- HDU 5779/BC 85D Tower Defence
- emacs添加cscope插件
- 1100. Mars Numbers (20)
- 心态和想法,是提高编程水平的关键
- 使用Fiddler截断更改Request数据
- 湖南省第十届大学生计算机程序设计竞赛(原题) 第c题 酷酷的单词
- swift - closure