标签:
Description
You task is 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! = 120, 120 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
题意:寻找最小的自然数N,使其阶乘的末尾有Q个0.
思路:只有2和5相乘时末尾才会出现0,在阶乘过程中,因子2的个数足够多,所以每遇到一个5,末尾就会出现一个0,因此问题转化为求1到N这N个整数中包含了多少个因子5。
#include<stdio.h> #include<string.h> #define MAX 0x3f3f3f #define LL long long LL fun(LL x) { LL ans=0; while(x) { ans+=x/5; x=x/5; } return ans; } int main() { int n,k=1; LL m; scanf("%d",&n); while(n--) { scanf("%lld",&m); LL left=0; LL right=400000010; LL mid; while(left<=right) { mid=(left+right)>>1; if(fun(mid)>=m) right=mid-1; else left=mid+1; } printf("Case %d: ",k++); if(fun(left)!=m) printf("impossible\n"); else printf("%lld\n",left); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4823114.html