sgu 154
Factorial
题意:能否找到一个数,它的阶乘后面0的个数为n?
数越大,阶乘后的0越多。用二分找。对于一个数x,它的阶乘,将小于等于它的数分解质因数。其中2的个数一定大于5的个数。因此计5的个数就是结果末尾0的个数。比它小的数有x/5个5的倍数,x/25个25的倍数。那么5的数量就是x/5+x/25......
//#pragma comment(linker,"/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #include <stack> using namespace std; typedef long long lon; const lon SZ=120,INF=0x7FFFFFFF; lon arr[SZ]; lon getn(lon x) { lon res=0; for(int i=1;i<=12;++i) { res+=x/arr[i]; } return res; } int main() { //std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); //for(;scanf("%d",&n)!=EOF;) lon casenum; //cin>>casenum; //for(lon time=1;time<=casenum;++time) { lon n; cin>>n; arr[1]=5; for(int i=2;i<=12;++i)arr[i]=arr[i-1]*5; lon lo=1,hi=1e12+10; for(;lo<hi;) { lon mid=(lo+hi)/2; if(getn(mid)<n)lo=mid+1; else hi=mid; } if(getn(lo)==n)cout<<lo<<endl; else cout<<"No solution"<<endl; } return 0; }