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 ...
随机推荐
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- [转]sed常用命令总结
转自:http://blog.chinaunix.net/uid-26963748-id-3249732.html 一.Sed简介 Sed:Stream Editor 流式编辑器 又称行编辑器,每次 ...
- [转]android 获取 imei号码
核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)) .getDeviceId(); 1.加入权限 在manife ...
- js 计算时间差
function GetDateDiff(startDate,endDate) { var startTime = new (<any>Date)(Date.parse(startDate ...
- IIS添加映射配置
这种问题主要出现在使用应用程序级别的地址重写.如果你将一个动态的地址重写成虚拟的其它扩展名或者不带扩展名的地址,通常在IIS5.1和II6.0中,访问这样一个实际不存在的地址,首先会被Web服务器返回 ...
- 【技术累积】【点】【java】【30】代理模式
基础 代理模式是Java常见的设计模式之一.所谓代理模式是指客户端并不直接调用实际的对象,而是通过调用代理,来间接的调用实际的对象. 什么是代理 参考现实生活中的代理 比如某个品牌的某个省的代理商,作 ...
- (转)Arcgis for JS之Cluster聚类分析的实现
http://blog.csdn.net/gisshixisheng/article/details/40711075 在做项目的时候,碰见了这样一个问题:给地图上标注点对象,数据是从数据库来的,包含 ...
- centos安装指定mysql
mysql下载地址:http://repo.mysql.com/ nginx下载地址 我下载是这个 http://nginx.org/packages/centos/7/noarch/RPMS/ngi ...
- Spring Boot 集成 JWT 实现单点登录授权
使用步骤如下:1. 添加Gradle依赖: dependencies { implementation 'com.auth0:java-jwt:3.3.0' implementation('org.s ...
- iptables详解(2):iptables实际操作之规则查询
所属分类:IPtables Linux基础 在阅读这篇文章之前,请确保你已经阅读了如下文章,如下文章总结了iptables的相关概念,是阅读这篇文章的基础. 图文并茂理解iptables 如果你是一 ...