【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
2016-08-23 21:42
555 查看
1138 - 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.
题解:就是看看有没有一个数的阶乘满足有n个0,有输出这个数,没有输出impossible.
题解:和之前做的一道题一样就是看5有多少个因为0的形成是由10或者是2*5产生的呀!
开始很纠结不明白为什么5个0是不可能的,以为25!就有5个0了,后来才想到25!会形成6个0,因为25=5*5呀!!!不要忽视啊!!!
打表会超内存的,所以这里二分,简单二分,看看就懂啦!
最后要记得再检查一下,从样例就可以发现第三组输出了25,在检查一次就ok啦!
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define LL long long
LL n;
LL check(LL x)
{
LL sum=0;
while(x)
{
sum+=x/5;
x/=5;
}
return sum; //求 x!会有多 0
}
int main()
{
LL u,ca=1;
scanf("%d",&u);
while(u--)
{
scanf("%lld",&n);
LL L=1,R=10000000000;
while(R>=L)
{
LL mid=(L+R)>>1;
if(check(mid)>=n)
R=mid-1;
else
L=mid+1;
}
printf("Case %d: ",ca++);
LL ant=0;
R=L;
while(L)
{
ant+=L/5;
L/=5;
}
if(ant==n)
printf("%lld\n",R);
else
printf("impossible\n");
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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个0,有输出这个数,没有输出impossible.
题解:和之前做的一道题一样就是看5有多少个因为0的形成是由10或者是2*5产生的呀!
开始很纠结不明白为什么5个0是不可能的,以为25!就有5个0了,后来才想到25!会形成6个0,因为25=5*5呀!!!不要忽视啊!!!
打表会超内存的,所以这里二分,简单二分,看看就懂啦!
最后要记得再检查一下,从样例就可以发现第三组输出了25,在检查一次就ok啦!
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define LL long long
LL n;
LL check(LL x)
{
LL sum=0;
while(x)
{
sum+=x/5;
x/=5;
}
return sum; //求 x!会有多 0
}
int main()
{
LL u,ca=1;
scanf("%d",&u);
while(u--)
{
scanf("%lld",&n);
LL L=1,R=10000000000;
while(R>=L)
{
LL mid=(L+R)>>1;
if(check(mid)>=n)
R=mid-1;
else
L=mid+1;
}
printf("Case %d: ",ca++);
LL ant=0;
R=L;
while(L)
{
ant+=L/5;
L/=5;
}
if(ant==n)
printf("%lld\n",R);
else
printf("impossible\n");
}
return 0;
}
相关文章推荐
- 【Lightoj 1282 Leading and Trailing】
- 【Light-oj】-1282 - Leading and Trailing(数论,快速幂,log,好)
- Light-oj 1282 - Leading and Trailing
- Lightoj 1282 (Leading and Trailing)
- Light OJ:1282 Leading and Trailing(数学+思维+对数+快速幂)
- hdu4458Shoot the Airplane+计算几何
- Light-oj-1282 Leading and Trailing(数学取位)
- lightoj-【1138 - Trailing Zeroes (III)】
- lightoj- 1138 - Trailing Zeroes (III)【思维】【二分查找】
- Lightoj1282(HDOJ1060) - Leading and Trailing
- lightoj 1282 - Leading and Trailing (数学--log使用)
- Fixing Insufficient resources to satisfy configured failover level for HA
- LightOJ 1138 Trailing Zeroes (二分+阶乘分解+思维)
- Codeforces Round #368 (Div. 2) A Brain's Photos(水)
- Lightoj 1138 ( Trailing Zeroes (III))
- lightoj Trailing Zeroes (III)
- lightoj Leading and Trailing
- 解决crond引发大量sendmail、postdrop进程问题
- 【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)
- LightOJ 1138 - Trailing Zeroes (III)【二分】