51Node 1010 只包含因子2 3 5的數 思維
阿新 • • 發佈:2019-01-07
題目
題意
思路
首先枚舉出所有滿足條件且不超過的數字,然後二分。
程式碼
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100000 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double PI = acos(-1);
LL arr[N];
int init(LL limit)
{
int index2 = 0, index3 = 0, index5 = 0;
int len = 0;
arr[0] = 1;
while(arr[len] <= limit)
{
arr[++len] = min({arr[index2]*2, arr[index3]*3, arr[index5]*5});
while(arr[index2]*2 <= arr[len]) ++index2;
while(arr[index3]*3 <= arr[len]) ++index3;
while (arr[index5]*5 <= arr[len]) ++index5;
}
return len;
}
int main()
{
int len = init(1e18 + 10);
int T;
LL n;
scanf("%d", &T);
while(T--)
{
scanf("%lld", &n);
printf("%lld\n", *lower_bound(arr+1, arr+len+1, n));
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100000 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double PI = acos(-1);
LL arr[N];
int init(LL limit)
{
int len = 0;
for(LL i = 1; i < limit; i *= 2)
{
for(LL j = 1; i*j < limit; j *= 3)
{
for(LL k = 1; i*j*k < limit; k *= 5)
arr[len++] = i*j*k;
}
}
sort(arr, arr + len);
return len;
}
int main()
{
int len = init(1e18 + 100);//注意,這裡偷懶用了浮點數形式,但超過15位後精度損失很大,填1e18+10得出的是10^18,填1e18+100得出的18^18+128
//cout << arr[len-1] << endl;
int T;
LL n;
scanf("%d", &T);
while(T--)
{
scanf("%lld", &n);
printf("%lld\n", *lower_bound(arr+1, arr+len, n));
}
return 0;
}