Trailing Zeroes (III) -;lightoj 1138
2015-08-21 09:12
330 查看
Trailing Zeroes (III)
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.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
PROBLEM SETTER: JANE ALAM JAN
题意:如果某个数的阶乘有n个0,问这个数最小可以是多少。
题目描述:
假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少?
解题思路:
由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n=2^x=5^y,可知x<<y。
所以我们可以直接根据因子5的个数,算阶乘末尾的零的个数。1<=Q<=10^8,所以可以用二分快速求解。
代码:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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个0,问这个数最小可以是多少。
题目描述:
假设有一个数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 = 0; while(n) { ans += n/5; n /= 5; } return ans; } int main() { int t, l = 1; scanf("%d", &t); while(t--) { long long n; scanf("%lld", &n); long long mid, low = 4, high = 500000000; while(low <= high) // 二分查找快~ { mid = (low+high)/2; long long num = slove(mid); if(num >= n) high = mid-1; else low = mid+1; } if(slove(low) == n) printf("Case %d: %lld\n", l++, low); else printf("Case %d: impossible\n", l++); } return 0; }
相关文章推荐
- 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- HUST team contest #E A Mountain Road||poj 3846 (dp)
- 阻塞套接字返回EAGAIN
- 开源SIP服务器 Kamailio
- 开源SIP服务器 Kamailio
- UVA - 11374 Airport Express(dijkstra)
- 内核中返回结构体首地址的函数container_of
- 不使用main方法入口,在控制台打印“hello world”
- [翻译] EAIntroView
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- awaitTermination() shutdown()
- Solr DataImportHandler 配置
- hdu 4300 Clairewd’s message
- error: Refusing toundefine while domain managed save image exists
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
- HDU 5414 CRB and String (2015 Multi-University Training Contest 10 2015多校联合)
- hdu 5414 CRB and String(2015 Multi-University Training Contest 10)
- 2012 Multi-University Training Contest 5 Problem G The Beautiful Road(HDU4346)
- 2012 Multi-University Training Contest 5 Problem F Permutation(HDU4345)