您的位置:首页 > 其它

light--oj--1116--Ekka Dokka(数学问题)

2015-12-04 21:24 197 查看
Description

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,让你将其拆成两个整数的积的形式,其中一个整数为奇数,另一个为偶数。如果存在多组解,找到偶数最小的那组解。

思路:尽量的剪枝,使算法的时间能够更短一些,不至于出现超时的事情。

ac代码:

//对数据进行分析,发现只要是奇数肯定不符合条件,偶数的话肯定能写成1*N的形式一定符合。
#include<stdio.h>
//注意使用long long型来进行定义
int main(){
int T,cnt=0;
scanf("%d",&T);
while(T--){
long long w;
long long u,v;
scanf("%lld",&w);
if(w%2){//奇数一定不满足条件
printf("Case %d: Impossible\n",++cnt);
continue ;
}
for(int i=2;i<=w;i++){//偶数的话进行遍历。
if(w%i)
continue;
u=w/i;
if(u%2&&i%2==0){
v=i;
break;
}
}
printf("Case %d: %lld %lld\n",++cnt,u,v);
}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: