HDU5878

http://acm.hdu.edu.cn/showproblem.php?pid=5878

给出你一个数字,让你求出大于这个数字n并且是形如2^a*3^b*5^c*7^d的最小的数;

就是用打表法求出所有的数,然后通过二分来查找,否则就会超时

有一点是童鞋们需要注意的,就是在文件中自定义函数的pow返回值是double型的,在这里我们需要用的返回值是int型的,要不然就会因为系统内部的精度问而出错

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<math.h>
 6 using namespace std;
 7 long long int ll=1e9;
 8 long long int ss[500005];
 9 
10 long long int pow(long long int a, long long int b)
11  {
12     long long int ans = 1;
13      while(b)
14      {
15          if(b & 1)ans *= a;
16         a *= a;
17          b>>=1;
18      }
19      return ans;
20  }
21  
22 int main()
23 {
24     int t;
25   long long int ch;
26     cin>>t;
27     while(t--){
28             int p=0;
29         int n;
30         for(long long int i=0;i<31;i++)
31         for(long long int j=0;j<20;j++)
32             for(long long int k=0;k<14;k++)
33         for(long long int l=0;l<12;l++){
34             ch=pow(2,i)*pow(3,j);
35             if(ch>ll)break;
36             ch*=pow(5,k);
37             if(ch>ll)break;
38             ch*=pow(7,l);
39             if(ch>ll)break;
40             ss[p++]=ch;
41         }
42         sort(ss,ss+p);
43     cin>>n;
44     int l=0;
45     int r=p-1;
46     while(l<r){
47         int mid=l+(r-l)/2;
48         if(ss[mid]>=n){
49             r=mid;
50         }
51         else l=mid+1;
52     }
53     cout<<ss[l]<<endl;
54 
55     }
56 }
View Code
你若盛开,清风自来...
原文地址:https://www.cnblogs.com/shangjindexiaoqingnian/p/5901622.html