求阶乘是某个数的最大多少次方

Description
As we know n!=1*2*3*…*n, but n! could be extreme huge. For example when n=100 then n! is nearly 9.3*10^157.But now your task is not to calculate the exact value of n! .

Given two kind of operation

operation _A Q

operation _B Q

The operation _A is to calculate the number of rightmost Zeros in Q!. For Example, the number of rightmost zero in 5! (=120) is 1 and the number of rightmost Zeros in 10! (= 3628800) is 2. Note that the number of rightmost Zeros in 23! (= 25852016738884976640000) is 4.

The operation _B is to calculate the smallest number X such that the number of rightmost zero in X! is exactly Q. For example, the smallest number such that the number of rightmost zero in X! is exactly 1 is 5(because 5!=120). The smallest number such that the number of rightmost zero in X! is exactly 2 is 10(because 10!= 3628800).

Input
There are several test cases.
For each case, there are two integers OP, Q in a single line. (1<=OP<=2, 0<=Q<=10^9)

Output
Output a line indicates the case index (start from 1.) with format “Case index:” in a single line.

If OP=1 then output a single line indicates the zeros in the rightmost of Q!.

If OP=2 then output a single line indicates the smallest number X that the number of the rightmost zero in X! is exactly Q.
Output “No solution” (without quotes) in a single line if you could not find such X.

Sample Input
1 5
1 10
2 1
2 2
Sample Output
Case 1:
1
Case 2:
2
Case 3:
5
Case 4:
10

#include <cstdio>
#include <math.h>
using namespace std;
long long solve(long long n)
{
    long long  ans=0;
    while(n)
    {
        ans+=n/5;//可惜只可以意会:第一次除以5,第二次相当于除以了25,第三次相当于除以了125,在这个过程中5被计算了3次,25两次,125三次
        n/=5;
    }
    return ans;
}
long long  cal(long long n)
{
    long long  ans=-1;
    long long  low=0,high=1e10;//二分哦查找
    while(low<=high)
    {
        int mid=low+high>>1;
        if(solve(mid)==n)
        {
            ans=mid;
            high--;
        }
        else if(solve(mid)>n)
            high=mid-1;
        else
            low=mid+1;
    }
    return ans;
}
int main()
{
    long long op,n;
    int ase=1;
    while(scanf("%I64d%I64d",&op,&n)!=EOF)
    {
        printf("Case %d:\n",ase++);
        if(op==1)
            printf("%I64d\n",solve(n));
        else
        {
            long long ans=cal(n);
            if(solve(ans)!=n)
                puts("No solution");
            else
                printf("%I64d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值