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
", *s.lower_bound(N));
34     }
35     //system("pause");
36     return 0;
37 }
原文地址:https://www.cnblogs.com/zzqc/p/7424976.html