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;
}