LightOJ 1138 Trailing Zeroes (III) 打表

就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找

然后我看网上的都是直接二分找,真是厉害

#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e8;
const int INF=0x3f3f3f3f;
int cnt[800009],a[800009];
int main()
{
    int ans=0,pos=0;
    for(int i=5,j=1;;i+=5,++j){
      int t=i;
      while(t%5==0)++ans,t/=5;
      if(ans>=N){
        break;
      }
      if(j%100==0){
        cnt[++pos]=ans;
        a[pos]=i;
      }
    }
    cnt[++pos]=N+1;
    a[pos]=4*N+20;
    int cas=0,T;
    scanf("%d",&T);
    while(T--){
      int n,res=-1;
      scanf("%d",&n);
      int k=lower_bound(cnt+1,cnt+1+pos,n)-cnt;
      if(cnt[k]==n)res=a[k];
      else {
        --k;
        int l=5,sum=0;
        if(k!=0)l=a[k]+5,sum=cnt[k];
        for(;l<a[k+1];l+=5){
          int t=l;
          while(t%5==0)t/=5,++sum;
          if(sum>=n){
            break;
          }
        }
        if(sum==n)res=l;
      }
      printf("Case %d: ",++cas);
      if(res==-1)printf("impossible\n");
      else printf("%d\n",res);
    }
    return 0;
}
View Code

 

posted @ 2016-04-13 15:31  shuguangzw  阅读(128)  评论(0编辑  收藏  举报