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 "