[HDOJ5878]I Count Two Three(暴力枚举,二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878

两种做法

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 using namespace std;
20 #define fr first
21 #define sc second
22 #define cl clear
23 #define BUG puts("here!!!")
24 #define W(a) while(a--)
25 #define pb(a) push_back(a)
26 #define Rint(a) scanf("%d", &a)
27 #define Rll(a) scanf("%I64d", &a)
28 #define Rs(a) scanf("%s", a)
29 #define Cin(a) cin >> a
30 #define FRead() freopen("in", "r", stdin)
31 #define FWrite() freopen("out", "w", stdout)
32 #define Rep(i, len) for(int i = 0; i < (len); i++)
33 #define For(i, a, len) for(int i = (a); i < (len); i++)
34 #define Cls(a) memset((a), 0, sizeof(a))
35 #define Clr(a, x) memset((a), (x), sizeof(a))
36 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
37 #define lrt rt << 1
38 #define rrt rt << 1 | 1
39 #define pi 3.14159265359
40 #define RT return
41 #define lowbit(x) x & (-x)
42 #define onecnt(x) __builtin_popcount(x)
43 typedef long long LL;
44 typedef long double LD;
45 typedef unsigned long long ULL;
46 typedef pair<int, int> pii;
47 typedef pair<string, int> psi;
48 typedef pair<LL, LL> pll;
49 typedef map<string, int> msi;
50 typedef vector<int> vi;
51 typedef vector<LL> vl;
52 typedef vector<vl> vvl;
53 typedef vector<bool> vb;
54
55 const int maxn = 33;
56 int n;
57 int x2[maxn], x3[maxn], x5[maxn], x7[maxn];
58 vector<int> ret;
59
60 signed main() {
61     // FRead();
62     Cls(x2); Cls(x3); Cls(x5); Cls(x7); ret.clear();
63     x2[0] = 1; x3[0] = 1; x5[0] = 1; x7[0] = 1;
64     x2[1] = 2; x3[1] = 3; x5[1] = 5; x7[1] = 7;
65     ret.push_back(1);
66     ret.push_back(2);
67     ret.push_back(3);
68     ret.push_back(5);
69     ret.push_back(7);
70     For(i, 2, 31) {
71         x2[i] = x2[i-1] * 2;
72         x3[i] = x3[i-1] * 3;
73         x5[i] = x5[i-1] * 5;
74         x7[i] = x7[i-1] * 7;
75     }
76     For(a, 0, 31) {
77         For(b, 0, 31) {
78             For(c, 0, 31) {
79                 For(d, 0, 31) {
80                     int tmp = x2[a]*x3[b]*x5[c]*x7[d];
81                     if(tmp > 0 && tmp <= 1000000000LL) {
82                         ret.push_back(tmp);
83                     }
84                 }
85             }
86         }
87     }
88     sort(ret.begin(), ret.end());
89     int T;
90     Rint(T);
91     W(T) {
92         scanf("%I64d", &n);
93         printf("%I64d\n", *lower_bound(ret.begin(), ret.end(), n));
94     }
95     RT 0;
96 }

1

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 using namespace std;
20 #define fr first
21 #define sc second
22 #define cl clear
23 #define BUG puts("here!!!")
24 #define W(a) while(a--)
25 #define pb(a) push_back(a)
26 #define Rint(a) scanf("%d", &a)
27 #define Rll(a) scanf("%I64d", &a)
28 #define Rs(a) scanf("%s", a)
29 #define Cin(a) cin >> a
30 #define FRead() freopen("in", "r", stdin)
31 #define FWrite() freopen("out", "w", stdout)
32 #define Rep(i, len) for(int i = 0; i < (len); i++)
33 #define For(i, a, len) for(int i = (a); i < (len); i++)
34 #define Cls(a) memset((a), 0, sizeof(a))
35 #define Clr(a, x) memset((a), (x), sizeof(a))
36 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
37 #define lrt rt << 1
38 #define rrt rt << 1 | 1
39 #define pi 3.14159265359
40 #define RT return
41 #define lowbit(x) x & (-x)
42 #define onecnt(x) __builtin_popcount(x)
43 typedef long long LL;
44 typedef long double LD;
45 typedef unsigned long long ULL;
46 typedef pair<int, int> pii;
47 typedef pair<string, int> psi;
48 typedef pair<LL, LL> pll;
49 typedef map<string, int> msi;
50 typedef vector<int> vi;
51 typedef vector<LL> vl;
52 typedef vector<vl> vvl;
53 typedef vector<bool> vb;
54
55 inline bool scan_d(LL &num) {
56     char in;bool IsN=false;
57     in=getchar();
58     if(in==EOF) return false;
59     while(in!=‘-‘&&(in<‘0‘||in>‘9‘)) in=getchar();
60     if(in==‘-‘){ IsN=true;num=0;}
61     else num=in-‘0‘;
62     while(in=getchar(),in>=‘0‘&&in<=‘9‘){
63         num*=10,num+=in-‘0‘;
64     }
65     if(IsN) num=-num;
66     return true;
67 }
68
69 const int maxn = 33;
70 LL n;
71 LL k[5] = {2,3,5,7};
72 vector<LL> ret;
73
74 void dfs(LL cur, int depth) {
75     if(depth == 4) ret.push_back(cur);
76     else {
77         for(LL i = 1; cur * i <= 1000000000LL; i *= k[depth]) {
78             dfs(LL(cur*i), depth+1);
79         }
80     }
81 }
82
83 signed main() {
84     // FRead();
85     int T;
86     Rint(T);
87     dfs(1LL, 0);
88     sort(ret.begin(), ret.end());
89     W(T) {
90         scan_d(n);
91         printf("%I64d\n", *lower_bound(ret.begin(), ret.end(), n));
92     }
93     RT 0;
94 }

2

时间: 2024-10-03 17:04:01

[HDOJ5878]I Count Two Three(暴力枚举,二分)的相关文章

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

蓝桥杯 2014本科C++ B组 奇怪的分式 暴力枚举

蓝桥杯 枚举 奇怪的分式 标题:奇怪的分式 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是: 1/4 乘以 8/5 小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png) 老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼! 对于分子.分母都是 1~9 中的一位数的情况,还有哪些算式可以这样计算呢? 请写出所有不同算式的个数(包括题中举例的). 显然,交换分子分母后,例如:4/1 乘以 5/8 是满足要求的,这算做不同的算式. 但对于分子分母相同的情况

hdu 4932 Miaomiao&#39;s Geometry 暴力枚举

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 694    Accepted Submission(s): 180 Problem Description There are N point

CODE FESTIVAL 2017 qual A--B-fLIP(换种想法,暴力枚举)

个人心得:开始拿着题目还是有点懵逼的,以前做过相同的,不过那是按一个位置行列全都反之,当时也是没有深究.现在在打比赛不得不 重新构思,后面一想把所有的状态都找出来,因为每次确定了已经按下的行和列后,按不同的操作所加的数都是一样的,于是就想到了用set 暴力枚举,从1-n个分别行列按钮,然后再枚举不同操作即确定行时再对列进行操作,每次操作放入set就可以了. 题目: Problem Statement We have a grid with N rows and M columns of squa

hdu5616 暴力枚举

2017-08-25 20:08:54 writer:pprp 题目简述: ? HDU 5616? n个砝码,可以放在天平左右两侧或不放? m次询问,每次询问是否可以测出给定重量? 1 ≤ n ≤ 20? 1 ≤ m ≤ 100 这道题采用枚举的思路的话实现起来还是有点困难的, 要实现的功能是对每个砝码进行处理,加到左边, 加到右边,或者是不加 看了大神的代码,感觉很巧妙, 设置了两个标记数组 vis1[2005], vis2[2005] 一个vis1用来记录当前已经可以实现的重量 另一个vis

hdu4282A very hard mathematic problem 暴力枚举

//给出k //找x,y,z使得x^z+y^z+x*y*z = k //x,y,z都为正整数x<y,z>1问有多少种方法 //当z = 2时,可以看到左边是一个完全平方 //而当z>=3时,可以暴力枚举x,y //由于k<2^31所以x<2^(31/3)枚举复杂度可以过 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> using name

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

ZOJ3818-Pretty Poem(暴力枚举)

题目链接 题意:求所给字符串是否符合ABABA或者ABABCAB的形式,如果可以的话输出Yes,不可以的话为No. 思路:暴力枚举A和B的长度,再用从长度减去3倍的AB长度,即为C的长度,看组合而成的字符串是否与给定的相等.在这里string中的substr函数是个好东西. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <a

uva10892(暴力枚举)

把n的所有因子求出来,总数不会太多,所以直接O(n2)的暴力枚举所有对行不行. 有几个细节要注意,详见代码. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set> #include<vector> #include<algorit