HDU - 5878 A - I Count Two Three H 技巧枚举

题意

输入 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值