LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdz20172133/article/details/86582960

1138 - Trailing Zeroes (III)
    PDF (English)    Statistics    Forum
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

PROBLEM SETTER: JANE ALAM JAN

题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。

分析:n!阶乘0的个数,就是求n!中5的重数,

具体解释:https://blog.csdn.net/sdz20172133/article/details/81448221

这题加一个二分就行

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const double eps = 1e-8;
const ll MOD = 1e9 + 7;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
ll cal(ll n,ll p)
{
    ll num=0;
	while (n)
	{
		num += n/p;
		n=n/ p;
	}
    return num;
}
int main()
{
     int t;
     int kase=0;
	  scanf("%d",&t);
	  while(t--)
	  {
	  	kase++;
	  	ll n;
	  	scanf("%lld",&n);
	  	ll l=1,r=1e18;
	  	//cout<<cal(25,5)<<endl;
	  	while(l<r)
		{
			ll mid=(l+r)>>1;
			if(cal(mid,5)>=n)
			{
				r=mid;
			}
			else 
			{
				l=mid+1;
			}
		}
		printf("Case %d: ",kase);
		if(cal(l,5)!=n)
		{
			printf("impossible\n");
		}	
		else
		printf("%lld\n",l);
	  }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/86582960