Trailing Zeroes (III) -;lightoj 1138
| 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 |
题目描述:
假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少?
解题思路:
由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n=2^x=5^y,可知x<<y。
所以我们可以直接根据因子5的个数,算阶乘末尾的零的个数。1<=Q<=10^8,所以可以用二分快速求解。
代码:
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; long long slove(long long n)
{
long long ans = ;
while(n)
{
ans += n/;
n /= ;
}
return ans;
} int main()
{
int t, l = ; scanf("%d", &t);
while(t--)
{
long long n;
scanf("%lld", &n);
long long mid, low = , high = ; while(low <= high) // 二分查找快~
{
mid = (low+high)/;
long long num = slove(mid);
if(num >= n)
high = mid-;
else
low = mid+;
}
if(slove(low) == n)
printf("Case %d: %lld\n", l++, low);
else
printf("Case %d: impossible\n", l++);
}
return ;
}
Trailing Zeroes (III) -;lightoj 1138的更多相关文章
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
1138 - Trailing Zeroes (III) PDF (English) problem=1138" style="color:rgb(79,107,114)" ...
- Trailing Zeroes (III)(lightoj 二分好题)
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Trailing Zeroes (III) 假设n!后面有x个0.现在要求的是,给定x,要求最小的n; 判断一个n!后面有多少个0,通过n/5+n/25+n/125+...
/** 题目:Trailing Zeroes (III) 链接:https://vjudge.net/contest/154246#problem/N 题意:假设n!后面有x个0.现在要求的是,给定x ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- 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 ...
- 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:如果没有输出 ...
随机推荐
- css 实现渐变
background:-ms-radial-gradient(circle,rgba(0, 0, 0, 0.2),rgba(0,0,0,0)); /* IE 10*/ background: -web ...
- python实现建立udp通信
实现代码如下: #udp协议通信import socket,timeclass UdpConnect: def get_udp(self,ip,port,message): #建立udp连接 myso ...
- 解决django项目在ubuntu系统上无法安装mysqlclient
首先我的项目是django2.0,python环境是3.5. 我们在本地开发完django项目了,在本地运行是成功的,然后我们把django项目放到服务器上,运行的时候就出错了. 如图: 我们都知道, ...
- C++期末
华侨大学 面向对象程序设计(二) 试卷(A) 系别 计算机.软件工程.网络工程08 考试日期 2009年 06月29日 姓名 学号 成绩 一.选择题 (20分,每小题2分) ()关于重载函数在调用时匹 ...
- python工程的结构
1 python系统库的位置 大部分系统库在/usr/lib64/python2.7目录下,但是像sys模块,是python内置的库,是用c实现的,直接连接进了python.exe中了. 也就是说,在 ...
- OOM排除与JVM调优
仅先记录,后续整理 1. 常用命令: jstat gcutil jmap 2. 打印GC执行情况: 通过执行jinfo -flag +PrintGCDetails <pid>直接动态开启, ...
- FastDFS搭建单机图片服务器(一)
防丢失转载:https://blog.csdn.net/MissEel/article/details/80856194 根据 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 和 ...
- Python时间模块datetime用法
时间模块datetime是python内置模块,datetime是Python处理日期和时间的标准库. 1,导入时间模块 from datetime import datetime 2,实例 from ...
- 3183 RMQ / 贪心(坑成。。)
题意:删去m个数,使剩下的数组成的数最小 题解 :贪心 , RMQ RMQ解法,建st表找,用rmq找最小值的下标,注意点 ,因为最小值是区间最右最小值,所以应该改成 <= 而不是< mi ...
- mysql中explain输出列之id的用法详解
参考mysql5.7 en manual,对列id的解释: The SELECT identifier. This is the sequential number of the SELECT wit ...