时间限制:0.25s
空间限制:4M
题意
你的任务是找到最小自然数 N, 使N!在十进制下包含 Q个零. 众所周知 N! = 1*2*...*N. 例如, 5! = 120, 120 结尾包含1个零.
Input
一个数 Q (0<=Q<=10^8).
Output
如果无解,输出"No solution" , 否则输出 N .
Sample test(s)
Input
2
Output
10
Solution:
二分答案。
#include <iostream> using namespace std; int check (int x) { int s = 0; while(x){ s+=x/5; x/=5; } return s; } int n; int main() { cin >> n; int l = 1, r = n *5, k, mid; int last = -1; if(n==0) last=1; while (l <= r) { mid = l + (r - l) / 2; k = check (mid); if (k == n) { last = mid; r = mid - 1; } else if (k > n) r = mid - 1; else if (k < n) l = mid + 1; } if (last != -1 ) cout << last; else cout << "No solution"; }
SGU 154.Factorial,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/keam37/p/3851815.html