题意
输入 t (1≤t≤500000), the number of test cases. t test cases follow. Each test case provides one integer n (1≤n≤109).让我们不小于n的最小的x满足 x=2^a*3^b*5^c*7^d
分析
这道题其实就是个枚举
但是不能随便枚举
比如 如果向这样枚举的话
scanf("%lld",&n);
for(ll i=n;;i++){
ll tem = i;
while(tem%2==0)tem/=2;
while(tem%3==0)tem/=3;
while(tem%5==0)tem/=5;
while(tem%7==0)tem/=7;
if(tem==1){
printf("%lld\n",i);
break;
}
}
那铁定是要TLE
case的数量是500000个 那平均输入数据都比较大 每个数据要差不多搜索1000个数才能找到我们所想要找的数的话 就会发现 这里500000*1000 = 500000000 关键有些数据还不会仅仅的查找1000次就找到
所以这样一定超时
我们看 既然枚举数字比较麻烦
我们如果枚举因子呢
我们已经知道 每个数的因子就是2,3,5,7
也就是说这个数一定能通过这四个数组合而成
如果我们通过枚举这四个因子的数量去做呢
比如这样
for(int f2=0;ff2<maxn;f2++,c++){
f2==0?ff2*=1:ff2*=2;
for(int f3 = 0;ff2*ff3<maxn;f3++,c++){
f3==0?ff3*=1:ff3*=3;
for(int f5=0;ff2*ff3*ff5<maxn;f5++,c++){
f5==0?ff5*=1:ff5*=5;
for(int f7=0;ff2*ff3*ff5*ff7<maxn;c++,f7++){
f7==0?ff7*=1:ff7*=7;
fac = ff2*ff3*ff5*ff7;
if(fac<maxn)s.insert(fac);
else break;
}
ff7=1;
}
ff5=1;
}
ff3=1;
}
那可是快多了
也就是8万多次循环就完成了
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e9+1;
int main()
{
ll fac=1,ff,c=0;
set<ll>s;
ll ff2=1,ff3=1,ff5=1,ff7=1;
for(int f2=0;ff2<maxn;f2++,c++){
f2==0?ff2*=1:ff2*=2;
for(int f3 = 0;ff2*ff3<maxn;f3++,c++){
f3==0?ff3*=1:ff3*=3;
for(int f5=0;ff2*ff3*ff5<maxn;f5++,c++){
f5==0?ff5*=1:ff5*=5;
for(int f7=0;ff2*ff3*ff5*ff7<maxn;c++,f7++){
f7==0?ff7*=1:ff7*=7;
fac = ff2*ff3*ff5*ff7;
// cout<<fac<<endl;
if(fac<maxn)s.insert(fac);
else break;
}
ff7=1;
}
ff5=1;
}
ff3=1;
}
// cout<<c<<endl; 81510
// cout<<s.size()<<endl; 7千多
int t;
scanf("%d",&t);
while(t--){
ll n;
scanf("%lld",&n);
printf("%lld\n",*(s.lower_bound(n)));
}
return 0;
}