Trailing Zeroes (III)-;lightoj 1138

Source: Internet
Author: User

Trailing Zeroes (III)
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 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: If the factorial of a number has n 0, ask what the minimum number of numbers can be.

Title Description:

Suppose there is a number n, it has a Q 0 at the end of the factorial, and now gives Q, Q, what is the minimum?

Problem Solving Ideas:

Since the number at the end of the 0 equals min (the number of factors 2, the number of factor 5), and because 2<5, then assume that there is an infinite number of n,n=2^x=5^y, known x<<y.

So we can calculate the number of the 0 at the end of the factorial directly according to the number of factor 5. 1<=q<=10^8, so it can be solved with two points quickly.

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 Long LongSlove (Long LongN)8 {9     Long LongAns =0;Ten      while(n) One     { AAns + = n/5; -N/=5; -     } the     returnans; - } -  - intMain () + { -     intT, L =1; +  Ascanf"%d", &t); at      while(t--) -     { -         Long LongN; -scanf"%lld", &n); -         Long LongMid, Low =4, high =500000000; -  in          while(Low <=High )//two min Find Fast ~ -         { toMid = (Low+high)/2; +             Long Longnum =Slove (mid); -             if(Num >=N) theHigh = mid-1; *             Else $Low = mid+1;Panax Notoginseng         } -         if(Slove (low) = =N) theprintf"Case %d:%lld\n", l++, low); +         Else Aprintf"Case %d:impossible\n", l++); the     } +     return 0; -}

Trailing Zeroes (III)-;lightoj 1138

Related Article

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.