LightOJ - 1116 Ekka Dokka

Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters where N is odd and M is even. Both N and M are positive integers.

They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer W (2 ≤ W < 263). And W will not be a power of 2.

Output

For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then print the result where M is as small as possible.

Sample Input

3

10

5

12

Sample Output

Case 1: 5 2

Case 2: Impossible

Case 3: 3 4

题目大意:给你一个数w,使n*m=w,n为奇数,m为偶数,使m尽可能的小

解题思路:

当w为奇数的肯定不可能,当w为偶数时一定可以,

当w为偶数时为了使偶数最小,也就是使奇数最大,这是可以使w一直除以2直到w为奇数,此时奇数最大,即得结果

AC代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
	ll w,t,n,m;
	cin>>t;
	for(int kase=1;kase<=t;kase++)
	{
		cin>>w;
		ll i=2,flag=0;
		if(w&1)
		printf("Case %d: Impossible\n",kase);
		else
		{
		 	n=w;
		 	while(n%2==0)
		 	{
		 		n/=2;
			}
			m=w/n;
			printf("Case %d: %lld %lld\n",kase,n,m);
		}
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_40707370/article/details/82556381