1375阶乘的零(求阶乘几个零和反推)
理论基础参考:阶乘后面几个0和证明:
1,https://blog.csdn.net/u011590573/article/details/80030594
2,https://blog.csdn.net/m0_37579232/article/details/79828286
代码参考:https://blog.csdn.net/rnzhiw/article/details/81258270
思维方法:找规律加二分法(感谢大佬)
求2016的阶乘后面有多少个零:
下面是解法:
5的倍数个数为: 2016/5 = 403个
25的倍数个数为: 403/5 = 80个
125的倍数的个数为:80/5 = 16个
625的倍数的个数为: 16/5 = 3个。
所以可以得出2016!后面0的个数为:403+80+16+3 = 502个.
---------------------
作者:rnzhiw
来源:CSDN
原文:https://blog.csdn.net/rnzhiw/article/details/81258270
版权声明:本文为博主原创文章,转载请附上博文链接!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
ll f(ll n){
ll num=0;
ll b=1;
while(1){
b*=5;
num+=n/b;
if(b>n)break;
}
return num;//这里返回的就是一个数n的阶乘的末尾的零的个数
}
int main()
{
ll p,sum,n;
while(~scanf("%lld",&n))
{
int flag=0;
ll right = 10000000000;
ll left = -5;
while(left<right){
ll mid = (right + left) / 2;
sum=f(mid);//中间位置
if(sum==n)
{
while(mid % 5 != 0)
mid--;//得到最小的数
printf("%lld\n",mid);
flag = 1;
break;
}
else if(sum > n) right = mid - 1;//输入的0个数偏小,改变右边界
else left = mid + 1;//改变左边界
}
if(flag == 0 )
printf("No solution\n");
}
return 0;
}