Trailing Zeroes (III)————二分+尺取练习

灰太狼 2022-05-17 19:56 442阅读 0赞

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 N ! = 1 ∗ 2 ∗ . . . ∗ N .
For example, 5!=120, 5 ! = 120 , 120 contains one zero on the trail.

Input
Input starts with an integerT(≤10000) T ( ≤ 10000 ) , denoting the number of test cases.

Each case contains an integer Q(1≤Q≤108) Q ( 1 ≤ Q ≤ 10 8 ) 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


让你找阶乘末尾0个数是Q的数最小是多少,
不存在的话就输出impossible
关于阶乘末尾0的个数我这里有一篇博客讲怎么求:n!末尾0的个数


具体看代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll MAXN=10000000000000;
  5. ll f(ll n)
  6. {
  7. ll ans=0;
  8. while(n) ans+=(n/=5);
  9. return ans;
  10. }
  11. int main()
  12. {
  13. int t;ll Q;
  14. scanf("%d",&t);
  15. int kase=1;
  16. while(t--)
  17. {
  18. scanf("%lld",&Q);
  19. ll l=1,r=MAXN;
  20. ll ans=0;
  21. while(r>=l)
  22. {
  23. ll mid=(l+r)>>1;
  24. if(f(mid)==Q)
  25. {
  26. ans=mid;
  27. // break;//不能直接退出,因为阶乘末尾0个数等于Q的有很多呢,还得继续找最小的
  28. r=mid-1;//
  29. }
  30. else if(f(mid)<Q) l=mid+1;
  31. else r=mid-1;
  32. }
  33. printf("Case %d: ",kase++);
  34. if(ans) printf("%lld\n",ans);
  35. else printf("impossible\n");
  36. }
  37. return 0;
  38. }

发表评论

表情:
评论列表 (有 0 条评论,442人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Pie————二分+练习

    给你n个蛋糕的半径,你有m个朋友,所以总共有m+1个人,现在要分蛋糕,要求每个人分到的大小都是一样的,且每个人的蛋糕都是从一块上切割下来的(不能是2个不同的蛋糕拼起来的),现在