sgu 154. Factorial 二分

154. Factorial

time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.

Input
One number Q written in the input (0<=Q<=10^8).

Output
Write "No solution", if there is no such number N, and N otherwise.

Sample test(s)

Input
2
Output
10


#include <bits/stdc++.h>

using namespace std;

int f(int x){
    int now=0;
    while(x>0){
        now+=x/5;
        x/=5;
    }
    return now;
}

void solve(){
    int q;
    scanf("%d",&q);
    int left=1,right=500000000,ans=500000005;
    while(left<=right){
        int mid=(left+right)>>1;
        if(f(mid)<q){
            left=mid+1;
        }
        else if(f(mid)==q){
            ans=min(ans,mid);
            right=mid-1;
        }
        else{
            right=mid-1;
        }
    }
    if(ans==500000005){
        printf("No solution");
        return;
    }
    printf("%d",ans);

}

int main(){
    solve();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章