时间:0.2 空间:32M
题目描述:
寻找一个最小的N,使得N!末尾恰好有Q个0
输入格式:
输入一个整数Q
输出格式:
如果有解输出一个整数N;否则输出”impossible”
样例输入1:
2
样例输出1:
10
样例输入2:
5
样例输出2:
impossible
约定:
1<=Q<=10^18
提示:
此题仔细观察,发现其实可以二分N,反过来验证Q,就可以了。judge函数是算X!末尾有几个0的,就是算因数5的个数,可以仔细体会一下。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#define L long long
#define CON 10000000000000
using namespace std ;
L Q , n ;
L judge( L x )
{
L w = 0 ;
while ( x >= 5 )
{
w += x / 5 ;
x /= 5 ;
}
return w ;
}
int main ( )
{
scanf ( "%lld" , & Q ) ;
L l = 1 , r = CON , mid , mrzf ;
while ( l < r )
{
// cout<<l<<" "<<r<<endl;
mrzf = 0 ;
mid = ( l + r ) / 2 ;
mrzf = judge ( mid ) ;
if ( mrzf < Q ) l = mid + 1 ;
else r = mid ;
}
mrzf = judge(r) ;
if ( mrzf != Q ) printf ( "impossible" ) ;
else printf ( "%lld" , r ) ;
return 0 ;
}
相关链接:
XJOI 3580 最大化最小值 题解:
https://blog.csdn.net/zj_mrz/article/details/80942079