LIGHT_OJ 1138 the number of lead zeros after factorial

Source: Internet
Author: User

LIGHT_OJ 1138 the number of leading zeros after factorial n-trailing zeroes (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%LLD &%llusubmit Status Practice LIGHTOJ 1138

Description

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

3

1

2

5

Sample Output

Case 1:5

Case 2:10

Case 3:impossible

Test instructions: Given n, the factorial q! of 0 number of n is obtained, the output Q, the output "impossible" is not present;

Train of thought: After guide zero, is looking for 2 and 5, because more than 5:2 so directly find 5,q! in how many factors 5 that is how many after guide zero, each 5 multiples contribute a post-zero, each of the multiples of 25 contribute a post-zero, each 5^3 contribute a post-derivative 0, and so on. Factorial q! after 0 number is f (q) =q/5+q/25+q/125+ ... Then the two decomposition function f (x) =n can be.

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<cctype>using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Constll inf= (1<< in);Const Doubleeps=0.0000000001;Const DoublePi=acos (-1.0); ll N;ll F (ll x) {ll res=0, t=5;  while(t<=x) {Res+=x/T; T*=5; }    returnRes;} ll Bin_search (ll left,ll right,ll key) { while(left<=Right ) {ll Mid= (left+right)/2; if(f (Mid) ==key&&f (mid-1) <key)returnmid; Else if(f (mid) <key) left=mid+1; Elseright=mid-1; }    return-1;}intMain () {intt,tag=1; CIN>>T;  while(t--) {cin>>N; ll ans=bin_search (1, Inf,n); printf ("Case %d:", tag++); if(ans!=-1) printf ("%lld\n", ans); ElsePuts"Impossible"); }    return 0;}
View Code

LIGHT_OJ 1138 the number of lead zeros after factorial

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.