Trailing Zeroes (III) (r二分)【LightOj】-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.

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


題意:大意就是說給一個整數n,n的階乘結果有幾個零,現在給出零的個數q,找出整數

代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAX 0x7fffffff    //最大的 long int型數  
using namespace std;
int jc(int n)  //計算n的階乘後0的個數 
{
	int ans=0;
	while(n!=0)
	{
		ans+=n/5;
		n/=5;
	}
	return ans;
}
int main()
{
    int t;
    cin >> t;
    int num=1;
    while(t--)
    {
    	int q;
    	cin >> q;
    	printf("Case %d: ",num++);
    	int left=0,right=MAX;
    	int mid;
    	while(left<=right)    //二分運算 
    	{
    		mid=(left+right)/2;
    		if(jc(mid)>=q)
    		right=mid-1;
    		else
            left=mid+1;
		}
		if(jc(left)==q)     //最後一次逼近的時候left==mid==right 
		cout << left <<endl;
		else
		printf("impossible\n");
	}
	return 0;	
}

//int cal(int n)      //計算n的階乘後0的個數  
//{  
//    int ans = 0;  
//    while (n)  
//    {  
//        ans += n/5;  
//        n /= 5;  
//    }  
//    return ans;  
//}   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章