题目链接:http://lightoj.com/volume_showproblem.php?problem=1138

题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0;如果没有输出impossible

可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可;

  1. LL Find(LL x)
  2. {
  3. LL ans = ;
  4. while(x)
  5. {
  6. ans += x/;
  7. x /= ;
  8. }
  9. return ans;
  10. }

例如125! 125/5 = 25;所以125!中含5的项为25*5+24*5+23*5+...+3*5+2*5+1*5 = 25!*5 

所以125!末尾有25+5+1 = 31 个0;

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <math.h>
  5. typedef long long LL;
  6. #define N 1000001
  7. using namespace std;
  8. const double eps = 1e-;
  9.  
  10. LL Find(LL x)
  11. {
  12. LL ans = ;
  13. while(x)
  14. {
  15. ans += x/;
  16. x /= ;
  17. }
  18. return ans;
  19. }
  20.  
  21. int main()
  22. {
  23. int T, t = , n;
  24. scanf("%d", &T);
  25. while(T --)
  26. {
  27. scanf("%d", &n);
  28. LL ans = , L = , R = ;
  29. while(L <= R)
  30. {
  31. LL Mid = (L+R)/;
  32. LL ret = Find(Mid);
  33.  
  34. if(ret == n)
  35. ans = Mid;
  36. if(ret >= n)
  37. R = Mid - ;
  38. else
  39. L = Mid + ;
  40. }
  41. if(ans == )
  42. printf("Case %d: impossible\n", t++);
  43. else
  44. printf("Case %d: %lld\n", t++, ans);
  45. }
  46. return ;
  47. }

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分的更多相关文章

  1. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

  2. LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)

    http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS     M ...

  3. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  5. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  6. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

  7. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  8. LightOJ 1138 Trailing Zeroes (III) 打表

    就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...

  9. Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

    1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

随机推荐

  1. Web前端性能优化之图片优化

    我自己的Blog:http://blog.cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到了互联网内容总量的62%,也就是说超过 ...

  2. object-c [self class] 和 [self _cmd]

    [self class] 返回当前类名 [self _cmd] 返回当前方法名 self 是类的隐藏的参数,指向当前当前调用方法的类 另一个隐藏参数是_cmd,代表当前类方法的selector

  3. HDU 4648 Magic Pen 6

    题目链接 6Y什么水平.. #include <cstdio> #include <cstring> #include <string> #include < ...

  4. Mina传递对象

    利用Apache MINA来传递对象,这对了MINA来说非常容易,并且这也是Java网络编程中很常用的应用. 首先看两个用来传递的Java对象MyRequestObject和MyResponseObj ...

  5. JBPM表达业务流程(流程定义语言)

    业务流程包括三部分: 活动 Activity / 节点 Node (有很多种,不同的类型有不同的功能,必须要有一个Start Activity) 连线 Transition / 转移 (从一个Acti ...

  6. javascript获取随机数的几种方式

    //获取0-num的随机数 function randomNum(num){ return Math.floor(Math.random()*num); } //获取start-end的随机数 fun ...

  7. php一些技巧函数

    current() next() prev() 范例 <?php $people = array("Peter", "Joe", "Glenn& ...

  8. 彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, Utf-8

    彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, U 1.ASCII: 0-127(128-255未使用),美国标准 2.IS0-8859-1(lati ...

  9. [故障处理]联想笔记本故障0x0000007B

    同事笔记本故障,莫名其妙的快捷方式就找不到了.开始程序中的内容也无法正常查看. 解决步骤: 1.怀疑用户配置的问题,新建一个用户,没有解决. 2.使用自带的一键恢复ThinkVantage,恢复后,重 ...

  10. GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告

    GDC2016[全境封锁(Tom Clancy's The Division)]对为何对应Eye Tracked System,以及各种优点的演讲报告 原文 4Gamer編集部:松本隆一 http:/ ...