HDU 5878 I Count Two Three 青岛网络赛

来源:互联网 发布:下载个淘宝买东西的 编辑:程序博客网 时间:2024/05/17 04:44

题意找出大于等于 n 的最小的 能表示为 2的a次 乘以 3的b次 乘以 5的c次 乘以 7的d次 的数。

n最大为1e9,符合条件的数也没几个,预处理出来,排个序,二分一下就好了。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <set>#include <map>#include <cmath>#include <cstdlib>#include <stack>#include <queue>using namespace std;typedef long long LL;const LL MAX = 1000000000;LL ans[20000];int cnt = 0;LL Power(LL a, LL b){LL ans = 1;while (b > 0){if (b&1)ans *= a;b >>= 1;a *= a;}return ans;}void init(){LL tmp;for(int a = 0; a <= 31; a++){for(int b = 0; b <= 19; b++){for(int c = 0; c <= 12; c++){for(int d = 0; d <= 11; d ++){tmp = Power(2, a)*Power(3, b);if(tmp > MAX) break;tmp *= Power(5, c);if(tmp > MAX) break;tmp *= Power(7, d);if(tmp > MAX) break;ans[++cnt] = tmp;}}}}}int bisearch(int x){if(x == 1) return 1;int l = 1, r = cnt + 1, m;while(l < r){m = (l + r) >> 1;if(ans[m] > x) r = m;else if(ans[m] == x) return ans[m];else l = m + 1;}return ans[l];}int main(){init();sort(ans + 1, ans + 1 + cnt);int T;scanf("%d", &T);while(T--){int tmp;scanf("%d", &tmp);printf("%d\n", bisearch(tmp));}    return 0;} 


0 0
原创粉丝点击