LightOJ - 1138 Trailing Zeroes (III)
来源:互联网 发布:中金公司 知乎 编辑:程序博客网 时间:2024/06/08 14:14
Submit Status uDebug
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
Source
5 10 15 25 35 75 125
5*1 5*2 5*3 5*5 5*7 5*5*3 5*5*5 (5的个数和下面0的最多个数相等)
5*2=10 10*2=20 15*2=30 25*4=100 35*4=140 75*4=300 125*8=1000
5! 5 0
10! 5、10 00
15! 5、10、15 000
20! 5、10、15、20 0000
25! 5、10、15、20、25 000000
27! 5、10、15、20、25 000000
这题很考验思维,一个数字包含5就可以出现一个0,除于5就是从1到这个数一共包含多少个5,递归求总和,二分就可以
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
int binary(int x);
int judge(int x);
int ans;
int main()
{
int t, x, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &x);
if(binary(x)==0)
{
printf("Case %d: impossible\n",ncase++);
}
else
{
printf("Case %d: %d\n",ncase++,ans);
}
}
return 0;
}
int binary(int x)
{
int l=0, r=500000000;
while(l<=r)
{
int mid=(l+r)/2;
if(judge(mid)>x)
{
r=mid-1;
}
else
{
l=mid+1;
}
}
r-=r%5;
if(judge(r)==x)
{
ans=r;
return 1;
}
return 0;
}
int judge(int x)
{
int cnt=0;
while(x)
{
cnt+=x/5;
x/=5;
}
return cnt;
}
- lightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- LightOJ - 1138 Trailing Zeroes (III)
- lightoj-【1138 - Trailing Zeroes (III)】
- 【LightOJ 1138 Trailing Zeroes (III)】
- LightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- lightoj Trailing Zeroes (III)
- Trailing Zeroes (III) LightOJ
- *lightoj 1138 Trailing Zeroes (III) | 二分+数学
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- lightoj 1138 - Trailing Zeroes (III)【二分】
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- Lightoj 1138 Trailing Zeroes (III) 【二分】
- LightOJ 1138 Trailing Zeroes (III)(二分)
- 打印星形符号
- Smith Numbers
- 状态模式
- HDU 5860 Death Sequence(递推)
- 查询python已导入的modules 以及 如何取消已导入的modues
- LightOJ - 1138 Trailing Zeroes (III)
- hive加载数据
- JVM Attach机制实现
- vijos1037搭建双塔(一维背包问题)
- 1026. 程序运行时间(15)
- web前端开发遇到问题记录
- 基于Yahoo网站性能优化的34条军规及自己的见解
- Sunshine’s city(lct+线段树)
- Triangle leetcode