The maximum number of times to find factorial is

Source: Internet
Author: User

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

Given 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 then 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's the number of rightmost zero in X! is exactly 2 (because 10!= 3628800).

Input
There is several test cases.
For each case, there is the integers OP, Q in a, and 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, indicates the zeros in the rightmost of q!.

If op=2 then output a single line indicates the smallest number X so the number of the rightmost zero in X! is exactly Q.
Output "No solution" (without quotes) in a, 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;//Pity can only be understood: the first time divided by 5, the second equivalent of divided by 25, the third equivalent of divided by 125, in the process 5 was calculated 3 times, 252 times, 1253 times n/=5;
} return ans;
    } Long Long cal (Long long N) {long long ans=-1;
        Long long low=0,high=1e10;//two points oh find 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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.