HDU 5878(打表)

题意:给出一个整数n, 找出一个大于等于n的最小整数m, 使得m可以表示为2^a*3^b*5^c*7^d

思路:四层for循环枚举+打表,最后lower_bound()。

注意:这里最好用printf,用cout计蒜客上交TLE了……不知道为什么我写了cout.tie(0)用cout还是那么慢。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=100000000;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll aa[1001000];

ll qpow(ll x,ll n)
{
    ll res=1;
	while(n>0)
	{
	   if(n%2==1)
	   {
	   	 res=res*x;
	   }
	   x=x*x;
	   n>>=1;
	}
	return res;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int t,n,tot=0;
    ll temp;
    
        
    for(int d=0;d<15;d++)
    {
        ll dv=qpow(7,d);
        for(int c=0;c<15;c++)
        {
            ll cv=qpow(5,c);
            for(int b=0;b<30;b++)
            {
                ll bv=qpow(3,b);
                for(int a=0;a<30;a++)
                {
                    ll av=qpow(2,a);
                    ll temp=dv*cv*bv*av;
                  //  if(c==0&&b==0&&d==1&&a==1)

                    //    cout<<<<endl;
                    if(temp<10000000000)
                    {
                        aa[tot++]=temp;
                       // cout<<temp<<endl;
                    }
                    else
                        break;

                }
            }
        }
    }
    cin>>t;
	sort(aa,aa+tot);
    while(t--)
    {
        cin>>n;
        printf("%d\n",*lower_bound(aa,aa+tot,n)<<endl;
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/81675006