Time Limit: 2 second(s) Memory Limit: 32 MB

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

Output for Sample Input

3

1

2

5

Case 1: 5

Case 2: 10

Case 3: impossible

题意:求出现一个最小的数的阶乘满足末尾有连续q个0。

思路:一个最小的数末尾要出现0一定要是能被5或10整除的,0的连续长度与数组大小正相关,故二分数字得解。

  1. #include<set>
  2. #include<map>
  3. #include<stack>
  4. #include<cmath>
  5. #include<queue>
  6. #include<cstdio>
  7. #include<string>
  8. #include<cstring>
  9. #include<iostream>
  10. #include<limits.h>
  11. #include<algorithm>
  12. #define mem(a,b) memset(a,b,sizeof(a))
  13. using namespace std;
  14. const int INF=1e9;
  15. typedef unsigned long long ll;
  16. typedef long long LL;
  17. int main()
  18. {
  19. int T,cases=;
  20. scanf("%d",&T);
  21. while(T--)
  22. {
  23. int q;
  24. LL ans=-;
  25. scanf("%d",&q);
  26. int l=,r=q*,mid;//为什么右端点是q*5,因为q*5>=ans.想一想为什么(hint:刚才那个序列。。。)
  27. while(l<=r)
  28. {
  29. mid=(l+r)/;
  30. int m=mid,wu=,count=;
  31. while(m>=wu)
  32. {
  33. int p=m;
  34. p/=wu;
  35. count+=p;
  36. wu*=;
  37. }
  38. if(count==q)
  39. {
  40. ans=mid;
  41. break;
  42. }
  43. else if(count>q)
  44. r=mid-;
  45. else
  46. l=mid+;
  47. }
  48. if(ans==-)
  49. printf("Case %d: impossible\n",cases++);
  50. else
  51. printf("Case %d: %lld\n",cases++,ans/*);
  52. }
  53. }

代码转自(https://blog.csdn.net/duan_1998/article/details/72566106)

Trailing Zeroes (III) LightOJ - 1138 二分+找规律的更多相关文章

  1. Trailing Zeroes (III) LightOJ - 1138 不找规律-理智推断-二分

    其实有几个尾零代表10的几次方但是10=2*510^n=2^n*5^n2增长的远比5快,所以只用考虑N!中有几个5就行了 代码看别人的: https://blog.csdn.net/qq_422797 ...

  2. Trailing Zeroes (III) LightOJ - 1138(二分)

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  3. Trailing Zeroes (III)(lightoj 二分好题)

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

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

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

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

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

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

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

  7. LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

    1138 - Trailing Zeroes (III) PDF (English) problem=1138" style="color:rgb(79,107,114)" ...

  8. 1138 - Trailing Zeroes (III) 二分

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

  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. [原]排错实战——使用process explorer替换任务管理器

    原Aha总结注册表process explorersysinternalsprocess monitor 前言 一般,我们会使用任务管理器查看系统中有哪些进程正在运行,强制杀掉某个进程.可是系统自带的 ...

  2. Java之线程通信的应用:经典例题:生产者/消费者问题

    /** * 线程通信的应用:经典例题:生产者/消费者问题 * * 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品, * 店员一次只能持有固定数量 ...

  3. 通过OAuth2.0 获取授权访问SF 用户数据

    站长资讯: 创建应用程序 新建应用程序   访问示例(Python+django) 环境准备: index.html 两种方式: 方式一:采用由用户授权,调用者无需知道SF的用户名与密码 方式二:直接 ...

  4. [TJOI2017]不勤劳的图书管理员(分块+树状数组)

    有一个数组开大会MLE开小会RE的做法:就是树套树,即树状数组套主席树,这种方法比较暴力,然而很遗憾它不能通过,因为其时空复杂度均为O(nlog2n). 想到一种不怎么耗内存,以时间换空间,分块!单次 ...

  5. shell_xtrabackup_backup_mysql

    #!/bin/bash# Name: xtra_back_mysql.sh# Desc:该脚本使用xtrabackup工具对mysql数据库进行增量备份,根据自己需求可以自行修改# Usage:./x ...

  6. 迅为IMX6Q开发板提供原理图_底板PCB_驱动程序源码_芯片和LCD数据手册_开发板环境_使用手册

      迅为IMX6开发板: Android4.4/6.0系统  Linux + Qt5.7系统  Ubuntu12.04系统 部分案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核心板 ...

  7. 基础篇十:模块介绍(--with-help_random_index_module)

    配置语法: 下面开始配置: cd /etc/nginx/conf.d/default.conf

  8. 52)PHP,加了单例模式的数据库代码

    <?php class db { public $host ;//= "localhost";//定义默认连接方式 public $User;//= "root&q ...

  9. 48)PHP,工厂模式

    为啥需要工厂模式啊: (原来是生产类的工具——————————) 工厂类的代码格式: <?php class factory{ //Instance表示“实例”,“对象” static func ...

  10. java作业-----方法重载

    满足方法重载的条件:1.方法名相同    2.参数类型不同,参数个数不同,参数类型的顺序不同. 同时,方法的返回值不作为方法重载的判断条件.