Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
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。代表N!中末尾连续0的个数。让你求出最小的N。
求N!中尾连续0的个数:
LL change(LL x){
LL ans = 0;
while(x){
ans += x / 5;
x /= 5;
}
return ans;
}
AC代码
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long LL change(LL x){
LL ans = 0;
while(x){
ans += x / 5;
x /= 5;
}
return ans;
} int main (){
int T, n;
int k = 1;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
LL l = 0, r = 100000000 * 5 + 10;
LL mid, ans;
while(r > l){
mid = (l + r) / 2;
if(change(mid) >= n){
ans = mid;
r = mid;
}
else
l = mid + 1;
}
printf("Case %d: ", k++);
if(change(ans) == n)
printf("%lld\n", ans);
else
printf("impossible\n"); }
return 0;
}
Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】的更多相关文章
- Light oj 1138 - Trailing Zeroes (III) (二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的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: ...
- 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)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- 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,由于 ...
- LightOj 1138 Trailing Zeroes (III)
题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
随机推荐
- jmeter中beanshell断言的使用
简单使用beanshell的内容,进行测试内容的判断 这里通过断言内容,修改if的条件,达到发送警报邮件的功能 beanshell 代码如下: SampleResult 等效于 prev lo ...
- drawable的文件名大写
drawable的文件名大写导致的R文件消失!!!1
- Android 关于android.os.Build介绍
关于Build类的介绍 这个类为一个获取设备一些初始化信息的类,该类的主要信息都是通过一些static的字段获得: public static final String BOARD The name ...
- 【BZOJ2149】拆迁队(斜率优化DP+CDQ分治)
题目: 一个斜率优化+CDQ好题 BZOJ2149 分析: 先吐槽一下题意:保留房子反而要给赔偿金是什么鬼哦-- 第一问是一个经典问题.直接求原序列的最长上升子序列是错误的.比如\(\{1,2,2,3 ...
- 使用HBuilder新建项目
依次点击文件→新建→选择Web项目(按下Ctrl+N,W可以触发快速新建(MacOS请使用Command+N,然后左键点击Web项目)) 如上图,请在A处填写新建项目的名称,B处填写(或选择)项目保存 ...
- 347 Top K Frequent Elements 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- 自动保存草稿 asp+ajax自动存稿功能详解(转自影子)
自动保存草稿功能的原理 我们都知道网页是一种无状态的,每次都需要请求,响应,当一次请求完成后就与服务器断开连接了,所以我们不能像网页一样实现实时的交互功能,但是为了满足更多的需求一个比较无敌的程序员还 ...
- MVC系列学习(十一)-客户端的验证
1.通过一个实例,来了解MVC中强大的验证功能 1.1新建一个 [基本] 的mvc项目,因为要用到验证的js,然后在一个视图中写上一下代码,以及Model中的代码如下 [注]在调用html.EditF ...
- Laravel5.1学习笔记17 数据库3 数据迁移
介绍 建立迁移文件 迁移文件结构 执行迁移 回滚迁移 填写迁移文件 创建表 重命名/ 删除表 创建字段 修改字段 删除字段 建立索引 删除索引 外键约束 #介绍 Migrations are lik ...
- Android热修复方案比较
热修复的特点:无需重新发版,实时高效热修复:用户无感知修复,无需下载新的应用,代价小: 修复成功率高,把损失降到最低. 一.热修复开源方案和使用情况 方案名称 方案开发公司 开发时间 Github星评 ...