51nod 1010 stl/数论/二分

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010

1010 只包含因子2 3 5

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题

收藏

关注

K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。

所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。

例如:n = 13,S中 >= 13的最小的数是15,所以输出15。

Input

第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行1个数N(1 <= N <= 10^18)

Output

共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。

Input示例

5
1
8
13
35
77

Output示例

2
8
15
36
80这个就是筛丑数把,可以用stl里的容器操作,要筛10915个出来就好了一个也不能少不然会WA的,紫书例题。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<set>
 6 #include<ctime>
 7 #include<functional>
 8 #include<algorithm>
 9 using namespace std;
10 #define LL long long
11 LL num[5] = { 2,3,5 };
12 priority_queue<LL, vector<LL>, greater<LL> >q;
13 set<LL>s;
14 int main()
15 {
16     q.push(1);
17     s.insert(1);
18     bool ok = 1;
19     int sum = 0;
20     while (sum<=10915) {
21         LL u = q.top();q.pop();
22         for (int i = 0;i < 3;++i) {
23             LL x = u*num[i];
24             if (x<0 || x>1e18) { continue; }
25             if (!s.count(x)) { sum++;s.insert(x);q.push(x); }
26         }
27     }
28     LL T, N;
29     s.erase(1);
30     cin >> T;
31     while (T--) {
32         scanf("%lld", &N);
33         printf("%lld\n", *s.lower_bound(N));
34     }
35     //system("pause");
36     return 0;
37 }
时间: 2025-01-06 10:58:54

51nod 1010 stl/数论/二分的相关文章

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=

STL之二分查找 (Binary search in STL)

STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第45条的一个总结,阐述了各种查找算法的异同以及使用他们的时机. 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range.带有判别式的如count_i

POJ1845 数论 二分快速取余

大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题思路: 应用定理主要有三个: (1)   整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数 (2)   约数和公式: 对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 有A的所有因子之和为 S = (1+p1+p1^2+p1^3+...p1^k1

【C++】【STL】二分查找函数

binary_search 这个函数的返回值是布尔型,也就是最简单的找到了就为真,没找到就是假. 传入参数有三个,数据集合的左端点,数据集合的右端点,查找的值. 注意这些左端点右端点是要求左开右闭原则的,就是和数学上的左开右闭区间[a, b)一样,右端点是个不会被查阅的值. 一般来说写法类似: bool flag = false; int data[n] = {...};///数据 sort(data, data + n); flag = binary_search(data, data + n

51Nod 1010 只包含因子2 3 5的数 | 预处理+二分

Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 #include <bits/stdc++.h> using namespace std; typedef long long LL; #define rep(i,a,n) for(int i = a; i < n; i++) #define repe(i,a,n) for(int i =

51nod 1010 只包含因子2 3 5的数(打表+排序+二分)

1010 只包含因子2 3 5的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 取消关注 K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = 13,S中 >= 13的最小的数是15,所以输出15. Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000

51NOD 1010 只包含因子2 3 5的数(二分 + 预处理)

传送门 K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = 13,S中 >= 13的最小的数是15,所以输出15. Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 10000) 第2 - T + 1行:每行1个数N(1 <= N <= 10^18) Output 共T行,每行1个数,输出>

51nod 1010 只包含因子2 3 5的数 &amp;&amp; poj - 1338 Ugly Numbers(打表)

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 http://poj.org/problem?id=1338 首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中. 打表的时候要注意保持数组有序. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

【转】STL之二分查找 (Binary search in STL)

Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第45条的一个总结,阐述了各种查找算法的异同以及使用他们的时机. 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range.带有判别式的如count_if,find_if或者binary_search的派别式版本,其