zoukankan      html  css  js  c++  java
  • 51Nod 1010 只包含因子2 3 5的数

    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

    开始直接暴力试了一波,TLE。后面一想似乎可以先打表,再二分查找。打表还是一个技术活。

    //Asimple
    #include <bits/stdc++.h>
    #define swap(a,b,t) t = a, a = b, b = t
    #define CLS(a, v) memset(a, v, sizeof(a))
    #define debug(a)  cout << #a << " = "  << a <<endl
    #define test() cout<<"=========="<<endl
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> PII;
    const int maxn = 20000+5;
    ll n, m, res, ans, len, T, k, num, sum, t;
    ll a[maxn];
    
    void init(){
        ll er, san, wu;
        er = san = wu = 0;
        a[0] = 1;
        len = 0;
        while( a[len]<=1e18 ) {
            a[++len] = min(2*a[er], min(3*a[san], 5*a[wu]));
            if( a[len]==2*a[er] ) er ++;
            if( a[len]==3*a[san] ) san ++;
            if( a[len]==5*a[wu] ) wu ++;
        }
    }
    
    ll b_search(ll low, ll high, ll num){
        while( low<high ) {
            ll mid = (low+high)/2;
            if( a[mid]==num ) return num;
            else if(a[mid]<num ) low = mid+1;
            else high = mid;
        }
        return a[low];
    }
    
    void input() {
        ios_base::sync_with_stdio(false);
        init();
        cin >> T;
        while( T -- ) {
            cin >> n;
            ans = b_search(1, len, n);
            cout << ans << endl;
        }
    }
    
    int main(){
        input();
        return 0;
    }
  • 相关阅读:
    1094. Car Pooling
    121. Best Time to Buy and Sell Stock
    58. Length of Last Word
    510. Inorder Successor in BST II
    198. House Robber
    57. Insert Interval
    15. 3Sum java solutions
    79. Word Search java solutions
    80. Remove Duplicates from Sorted Array II java solutions
    34. Search for a Range java solutions
  • 原文地址:https://www.cnblogs.com/Asimple/p/7658412.html
Copyright © 2011-2022 走看看