zoukankan      html  css  js  c++  java
  • 只包含因子2 3 5的数(数论,二分,加丑数思想)

    个人心得:这题错了很多很多次,一开始单纯是想一直除2,3,5能除尽就可以了,但是数据太大,从第九组数据开始就超时了。

    后面听了队友的意见打表,这里用了丑数的思想,就是从2,3,5开始依次取出最小的数分别乘以2,3,5若不存在就放进优先队列,

    当然要用set就行存储,s.count()就是查找是否存在的函数,可惜还是超时了,应该是stl中的lower_bound函数效率太低,

    没办法就只能用另外的数组存储然后自己根据二分写查找函数,后面对longlong进行适当的输出就过了。

    题目:

    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
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<cstdio>
     5 #include<vector>
     6 #include<cmath>
     7 #include<stack>
     8 #include<set>
     9 #include<queue>
    10 #include<algorithm>
    11 using namespace std;
    12 #define in 1000000005
    13 priority_queue<long long,vector<long long >,greater<long long > >pq;
    14 set<long long >s;
    15 int flag=0;
    16 long long number[12000];
    17 bool pandu(long long x,long long y)
    18 {
    19     if((x-y)>=0) return true;
    20     else return false;
    21 }
    22 void dp()
    23 {
    24     int next[3]={2,3,5};
    25     for(int j=0;j<3;j++)
    26           {
    27               pq.push(next[j]);
    28               s.insert(next[j]);
    29           }
    30     int i=1;
    31     while(i<=12000)
    32     {
    33         long long t=pq.top();pq.pop();
    34         for(int j=0;j<3;j++){
    35             long long x=t*next[j];
    36             if(!s.count(x)) {s.insert(x);pq.push(x); i++;}
    37         }
    38 
    39     }
    40     set<long long >::iterator it;
    41     for(it=s.begin();it!=s.end();it++)
    42           {
    43               number[flag++]=*it;
    44           }
    45 }
    46 int finda(int a,int b,long long x)
    47 {
    48     int pos=0;
    49     int mid;
    50     while(a<b){
    51         mid=(a+b)/2;
    52         if(pandu(number[mid],x))
    53             {
    54                 b=mid;pos=b;
    55             }
    56         else
    57             {
    58             a=mid+1;
    59             pos=a;
    60             }
    61     }
    62     return pos;
    63 
    64 }
    65 int main()
    66 {
    67     int t;
    68    scanf("%d",&t);
    69     dp();
    70     while(t--){
    71         long long n;
    72         cin>>n;
    73         int t=finda(0,flag-1,n);
    74         cout<<number[t]<<endl;
    75 
    76     }
    77 
    78     return 0;
    79 }


  • 相关阅读:
    error:Unterminated &lt;form:form tag
    error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    mvc-dispatchar-servlet.xml文件报错
    修改MySQL密码
    Injection of autowired dependencies failed
    IDEA jsp模板
    jstl的表达式不能解析
    前端学习的小窍门
    Git的使用
    MySql数据库表的查询操作
  • 原文地址:https://www.cnblogs.com/blvt/p/7580741.html
Copyright © 2011-2022 走看看