Light OJ 1138-trailing Zeroes (III) "Two points to find a good question" "Give N!" At the end there is a continuous Q 0, which allows you to find the smallest n "

Source: Internet
Author: User

1138-trailing Zeroes (III)
PDF (中文版) Statistics Forum
Time Limit: 2 second (s) Memory Limit: MB

You task was to find minimal natural number N, so that n! contains exactly Q zeroes on the trail in decimal notation. As you know n! = 1*2*...*n. For example, 5! = contains one zero on the trail.

Input

Input starts with an integer T (≤10000), denoting the number of test cases.

Each case contains an integer Q (1≤q≤108) in a line.

Output

For each case, print the case number and N. If no solution is found then print ' impossible '.

Sample Input Output for Sample Input

3

1

2

5

Case 1:5

Case 2:10

Case 3:impossible

Problem Setter:jane ALAM JAN

Test instructions: give you a number q, which means n! The number of consecutive 0 in the end. Let you find the smallest n.

Theorem: Ask N! End ofnumber of consecutive 0
Find the following method
ll sum (ll N) {    ll ans = 0;    while (N)    {        ans + = N/5;        N/= 5;    }    return ans;}


Originally dared not write, finally found even q = 10^8 will not be super long long (seemingly int is not super)

Two again, the interval is small. WA once more.


AC code: Two-point implementation

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <vector> #include <map> #include <string> #include <algorithm># Define LL long long#define maxn 100+10#define maxm 20000+10#define INF 0x3f3f3f3fusing namespace std;    ll sum (ll N)//For the number of consecutive 0 at the end of the N factorial {ll ans = 0;        while (N) {ans + = N/5;    N/= 5; } return ans;    int k = 1;int main () {int t;    LL Q;    scanf ("%d", &t);        while (t--) {scanf ("%lld", &q);        ll left = 1, right = 1000000000000;//begins to open small drunk ll ans = 0;            while (right >= left) {int mid = (left + right) >> 1;                if (sum (mid) = = Q)//equal to the value to be assigned to ans {ans = mid;            right = Mid-1;            } else if (sum (mid) > Q) right = mid-1;        else left = mid + 1; } printf ("Case%d:", k++);        if (ans) printf ("%lld\n", ans);    else printf ("impossible\n"); } return 0;}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Light OJ 1138-trailing Zeroes (III) "Two points to find a good question" "Give N!" At the end there is a continuous Q 0, which allows you to find the smallest n "

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.