标签:
题意是让你求一个最小的N, 使得N!有M个0,N的阶乘中2的因子是远多于5的因子的个数, 因此我们统计出5的个数就知道其后面有几个0,对于一个数mid, mid!中5的因子的个数为mid/5 + mid/25 + mid/5^3 + ... 可以发现mid越大mid!中的5的个数越多, 因此我们可以二分答案, 代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; int M; int check(LL mid) //mid 的阶乘中5的个数 { LL a = 5; int res = 0; while(a <= mid) { res += mid/a; a *= 5; } return res; } int main() { int T; cin>>T; while(T--) { cin>>M; LL l=1, r=1000000000; LL res = -1; while(l <= r) { LL mid = (l+r)/2; int tp = check(mid); if(tp == M) { res = mid; r = mid-1; } else if(tp > M) r = mid - 1; else l = mid + 1; } if(res == -1) cout<<"No solution\n"; else cout<<res<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/xingxing1024/p/5215430.html