Lightoj Trailing Zeroes (III) 1138 "binary search + factorial decomposition"

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, this number represents n! There are several 0 behind. Give this number and calculate the value of N.


Problem Solving Ideas:

Any factorization can be written in the form of prime number multiplication. So calculate the factorial of a number after a few 0, just calculate how much this number contains 5. (The point about this is unclear: click the Open link).

You can use the dichotomy to find this point. The idea of using two points for this problem is no more difficult.

AC Code;

<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string > #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using Namespace Std;typedef long long LL;    ll solve (ll N) {ll num=0;        while (n) {NUM+=N/5;    n/=5; } return num;}    ll ER (ll N) {ll x=1;    LL y=1844674407370;    LL mid;    LL Res=-1;        while (y>=x) {mid= (x+y)/2;        LL Ans=solve (mid);            if (ans==n) {res=mid;            Y=mid-1;        return mid;        } else if (ans>n) {y=mid-1;        } else if (ans<n) {x=mid+1; }} return res;}    int main () {int t;    scanf ("%d", &t);    int xp=1;        while (t--) {LL n;        scanf ("%lld", &n);        LL ans=er (n);        if (ans==-1) printf ("Case%d:impossible\n", xp++);    else printf ("Case%d:%d\n", Xp++,ans); } return0;} </span>


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Lightoj Trailing Zeroes (III) 1138 "binary search + factorial decomposition"

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.