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"