http://lightoj.com/volume_showproblem.php?problem=1138

Trailing Zeroes (III)

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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使N!中0的个数等于q

0是有5乘4、2、8等等构成的,N中只要有因子5,那么N!中一定能构成0,所以我们只需要找N中因子5的个数,然后用二分来快速查找N

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10. ll solve(ll n)
  11. {
  12. ll ans = ;
  13. while(n)
  14. {
  15. ans += n / ;
  16. n /= ;
  17. }
  18. return ans;
  19. }//求n中因子5的个数
  20.  
  21. int main()
  22. {
  23. int t, p = ;
  24. ll q;
  25. scanf("%d", &t);
  26. while(t--)
  27. {
  28. p++;
  29. scanf("%lld", &q);
  30. ll low = , high = ;
  31. while(low <= high)
  32. {
  33. ll mid = (low + high) / ;
  34. ll m = solve(mid);
  35. if(m < q)
  36. low = mid + ;
  37. else
  38. high = mid - ;
  39. }//二分
  40. if(solve(low) == q)
  41. printf("Case %d: %lld\n", p, low);
  42. else
  43. printf("Case %d: impossible\n", p);
  44. }
  45. return ;
  46. }

LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)的更多相关文章

  1. 1138 - Trailing Zeroes (III) 二分

    1138 - Trailing Zeroes (III)   You task is to find minimal natural number N, so that N! contains exa ...

  2. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

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

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

  4. Light oj 1138 - Trailing Zeroes (III) (二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的N. 我们可以知 ...

  5. LightOJ 1138 Trailing Zeroes (III) 打表

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

  6. LightOj 1138 Trailing Zeroes (III)

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

  7. 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 ...

  8. light oj 1138 - Trailing Zeroes (III)【规律&&二分】

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

  9. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

随机推荐

  1. 不用写代码就能实现深度学习?手把手教你用英伟达 DIGITS 解决图像分类问题

    2006年,机器学习界泰斗Hinton,在Science上发表了一篇使用深度神经网络进行维数约简的论文 ,自此,神经网络再次走进人们的视野,进而引发了一场深度学习革命.深度学习之所以如此受关注,是因为 ...

  2. OpenCL 事件的使用,以及回调函数

    ▶ 事件的两种使用方法.第一种是用事件 a 标记进入命令队列的操作 A,于是后续进入命令队列的操作 B 可以被要求等到前面事件 a 完成(即操作 A 完成)以后才能开始调度执行.第二种是使用用户自定义 ...

  3. 10个免费的在线Markdown编辑器

    1. StackEdit StackEdit是一个很用特色的免费在线Markdown编辑器. 有一个非常不错的工具栏,可与云存储同步,以URL形式导入文件或者直接从硬盘打入.他还有一个亮点就是,可以减 ...

  4. Linux C多线程实现生产者消费者

    今天学习了用Linux C进行线程的同步,实现类似生产者消费者的问题.下面我就来分享我的代码 #include<stdio.h> #include<pthread.h> #in ...

  5. eclipse或tomcat web项目启动失败其中一种解决办法

    失败信息如下: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to s ...

  6. 存储过程返回update结果集和insert主键

    update teacher set name ='111' where id in(286,289);print @@rowcount;--或select将查出,是@@rowcount,不是@row ...

  7. 微信小程序-滑动视图注意事项

    真的得吐槽下微信的开发文档,一点点都不详细的好吗. <!--垂直滚动,这里必须设置高度--> <scroll-view scroll-y="true" style ...

  8. 基于Web Service的客户端框架搭建二:数据转换层(FCL)

    引言 要使用WebService来分离客户端与服务端,必定要使用约定好两者之间的数据契约.Json数据以其完全独立于语言的优势,成为开发者的首选.C# JavaScriptSerializer为Jso ...

  9. Kubernetes基本原理与示例

    1. Kubernetes介绍 基本概念 Pod Pod是Kubernetes的基本操作单元,把相关的一个或多个容器构成一个Pod,通常Pod里的容器运行相同的应用.Pod包含的容器运行在同一个Nod ...

  10. 如何让局域网其他电脑通过IP直接访问自己电脑的网站

    具体方法如下: 1. 打开系统的控制面板 2. 打开控制面板后打开window防火墙. 3.点击图中的“高级设置”选项. 4.点击图中的“本地计算机上的高级安全 Windows 防火墙”在右侧点击“W ...