LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138
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
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
typedef long long ll; ll solve(ll n)
{
ll ans = ;
while(n)
{
ans += n / ;
n /= ;
}
return ans;
}//求n中因子5的个数 int main()
{
int t, p = ;
ll q;
scanf("%d", &t);
while(t--)
{
p++;
scanf("%lld", &q);
ll low = , high = ;
while(low <= high)
{
ll mid = (low + high) / ;
ll m = solve(mid);
if(m < q)
low = mid + ;
else
high = mid - ;
}//二分
if(solve(low) == q)
printf("Case %d: %lld\n", p, low);
else
printf("Case %d: impossible\n", p);
}
return ;
}
LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)的更多相关文章
- 1138 - Trailing Zeroes (III) 二分
1138 - Trailing Zeroes (III) You task is to find minimal natural number N, so that N! contains exa ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- Light oj 1138 - Trailing Zeroes (III) (二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的N. 我们可以知 ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
- LightOj 1138 Trailing Zeroes (III)
题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...
- 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 ...
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
随机推荐
- Cloudera Manger CDH 安装文档
简介: Cloudera Manager 是 Cloudera 公司推出的 Hadoop 集群管理工具,通过该管理工具可以方便的部署.配置.监控集群. Cloudera 公司自己发布的 Hadoop ...
- 迷你MVVM框架 avalonjs 1.3.1发布
avalon1.3.1发布. interpolate支持注释节点做定界符,avalon.config({interpolate:["<!--","-->&qu ...
- 2015年传智播客JavaEE 第168期就业班视频教程11-导入配置文件
资源文件放在Source Folder目录下面,这个目录同样会编译到classes目录下 web.xml的 <listener> <listener-class>org.spr ...
- 18.4Sum (Map)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- sql产生随机数字
第一种:select cast(ceiling(rand() * 10) as int)第二种:select cast(ceiling(rand(checksum(newid()))*10) as i ...
- PHP半年了,已经可以独立支撑项目,几点心得记录
从去年12开始零基础学习PHP,到现在可以独立支撑项目,感谢PHP的强大,成熟.入门容易,记录几点心得: 1.思维比什么都重要,方法要靠实践证明: 2.多写.多试,不要怕遇到坑,每一个坑都是你前进路上 ...
- 修改RocketMQ的NameServer端口
---问题--- 有同事提出各个问题:如何修改RocketMQ的NameServer端口号?(默认:9876) ---结论--- 调查并验证之后,结论及过程如下: 验证版本:rocketmq-all- ...
- JavaScript 语法总结3
1. 数组初始化可以跳着来 var s = [1,2,,,,6]; // 中间省略的元素为undefined 2. 函数定义表达式: var f = function(args){ return ...
- S5PV210定时器
在S5PV210内部,一共有4类定时器件.这4类定时器件的功能.特征是不同的. 1.PWM定时器(1)这种是最常用的,平时所说的定时器一般指的是这个.像简单单片机(譬如51单片机)中的定时器也是这类. ...
- Java 高级基础——反射
Java 高级基础--反射 反射的意义:Java 强类型语言,但是我们在运行时有了解.修改信息的需求,包括类信息.成员信息以及数组信息. 基本类型与引用类型 基本类型,(固定的 8 种) 整数:byt ...