SGU 154. Factorial
154. Factorial
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard input
output: standard output
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
题目大意:给出一个数n的阶乘n!的末尾0的个数,求n最小是多少,如果找不到输出No solution
思路分析:由于2和5乘积会出现0,所以可以二分一个范围,是否存在一个数末尾0的个数满足要求
#include<iostream> using namespace std; int cal(int n) { int res=0; while(n) { res+=n/5; n/=5; } return res; } int main() { int q; while(cin>>q) { if(q==0) { cout<<1<<endl; continue; } int right=1000000003,left=1; int mid; while(left<right) { mid=(left+right)/2; if(cal(mid)<q) left=mid+1; else right=mid; } if(cal(left)==q) cout<<left<<endl; else cout<<"No solution"<<endl; } }