最简单二分查找,分析题意,某数阶乘的末尾有几个零,然而其实就是求阶乘展开式有5的几次方,一个5代表一个零。而且规律可得n个零时,5*n的阶乘中5的x次方大于等于n,顾二分初始边界取1和n,然后不断通过mid来判断,最后缩小范围然后确定。
题目:
问题 F: yifan and Zeroes
题目描述
One of My Teammates asd encountered a difficult problem recently. Given an integer q, he wants to now whether there is an integer n that n! contains q zeroes on the trail in decimal notation and n is minimal.
输入
Input starts with an integer T(T <= 20000),denoting the number of test cases. Each test case contains an integer q (1 <= q <= 10^8)
输出
For each case, print the case number and your answer, if no solution is found then print "I need anwei".
样例输入
3
1
2
5
样例输出
Case 1: 5
Case 2: 10
Case 3: I need anwei
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int s(int a)
{
int b=0;
a *= 5;
while(a > 0)
{
b += a/5;
a /= 5;
}
return b;
}
int main()
{
int t;
while(~scanf("%d", &t))
{
for(int j = 1; j <= t; j++)
{
int p;
scanf("%d", &p);
int l = 1, r = p;
while(l != r)
{
int mid = (l + r) / 2;
if(s(mid) < p && s(l)< p)
{
l = mid;
}
else if(s(mid) > p, s(r)> p)
{
r = mid;
}
if(r - l == 1)
break;
}
printf("Case %d: ", j);
if(s(l) == p)
printf("%d\n", l*5);
else if(s(r) == p)
printf("%d\n", r*5);
else
printf("I need anwei\n");
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int s(int a)
{
int b=0;
a *= 5;
while(a > 0)
{
b += a/5;
a /= 5;
}
return b;
}
int main()
{
int t;
while(~scanf("%d", &t))
{
for(int j = 1; j <= t; j++)
{
int p;
scanf("%d", &p);
int l = 1, r = p;
while(l != r)
{
int mid = (l + r) / 2;
if(s(mid) < p && s(l)< p)
{
l = mid;
}
else if(s(mid) > p, s(r)> p)
{
r = mid;
}
if(r - l == 1)
break;
}
printf("Case %d: ", j);
if(s(l) == p)
printf("%d\n", l*5);
else if(s(r) == p)
printf("%d\n", r*5);
else
printf("I need anwei\n");
}
}
return 0;
}