# Treasure Map

**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 335 Accepted Submission(s): 145
**

Problem Description

"Take 147 steps due north, turn 63 degrees clockwise, take 82 steps, …". Most people don’t realize how important accuracy is when following the directions on a pirate’s treasure map. If you’re even a tiny bit off at the start, you’ll

end up far away from the correct location at the end. Pirates therefore use very exact definitions. One step, for instance, has been defined by the 1670 Pirate Convention to be exactly two times the size of the wooden leg of Long John Silver, or 1.183 m in

metricunits.

Captain Borbassa was thus not at all worried when he set sail to the treasure island, having a rope with knots in it, exactly one step apart, for accurately measuring distances. Of course he also brought his good old geotriangle, once given to him by his father

when he was six years old.

However, on closer inspection of the map, he got an unpleasant surprise. The map was made by the famous captain Jack Magpie, who was notorious for including little gems into his directions.In this case, there were distances listed such as sqrt(33) steps. How

do you measure that accurately? Fortunately, his first mate Pythagor came to the rescue. After puzzling for a few hours, he came up with the following solution: let pirate A go 4 steps into the perpendicular direction, and hold one end of the measuring rope

there. Then pirate B goes into the desired direction while letting the rope slide through his fingers, until he is exactly 7 steps away from pirate A. Pythagor worked out a formula that states that pirate B has then traveled exactly sqrt(33) steps.

Captain Borbassa was impressed, but he revealed that there were more such distances on the map. Paranoid as he is, he refuses to let Pythagor see the map, or even tell him what other distances there are on it. They are all square roots of integers, that’s all

he gets to know. Only on the island itself will the captain reveal the numbers, and then he expects Pyhtagor to quickly work out the smallest two integer numbers of steps that can combine to create the desired distance, using the method described above.

Pythagor knows this is not easy, so he has asked your help. Can you help him by writing a program that can determine these two integers quickly? By the way, he did ask the captain how large the numbers inside the square root could get, and the captain replied

"one billion". He was probably exaggerating, but you’d better make sure the program works.

If you can successfully help the pirates, you’ll get a share of the treasure. It might be gold, it might be silver, or it might even be… a treasure map!

end up far away from the correct location at the end. Pirates therefore use very exact definitions. One step, for instance, has been defined by the 1670 Pirate Convention to be exactly two times the size of the wooden leg of Long John Silver, or 1.183 m in

metricunits.

Captain Borbassa was thus not at all worried when he set sail to the treasure island, having a rope with knots in it, exactly one step apart, for accurately measuring distances. Of course he also brought his good old geotriangle, once given to him by his father

when he was six years old.

However, on closer inspection of the map, he got an unpleasant surprise. The map was made by the famous captain Jack Magpie, who was notorious for including little gems into his directions.In this case, there were distances listed such as sqrt(33) steps. How

do you measure that accurately? Fortunately, his first mate Pythagor came to the rescue. After puzzling for a few hours, he came up with the following solution: let pirate A go 4 steps into the perpendicular direction, and hold one end of the measuring rope

there. Then pirate B goes into the desired direction while letting the rope slide through his fingers, until he is exactly 7 steps away from pirate A. Pythagor worked out a formula that states that pirate B has then traveled exactly sqrt(33) steps.

Captain Borbassa was impressed, but he revealed that there were more such distances on the map. Paranoid as he is, he refuses to let Pythagor see the map, or even tell him what other distances there are on it. They are all square roots of integers, that’s all

he gets to know. Only on the island itself will the captain reveal the numbers, and then he expects Pyhtagor to quickly work out the smallest two integer numbers of steps that can combine to create the desired distance, using the method described above.

Pythagor knows this is not easy, so he has asked your help. Can you help him by writing a program that can determine these two integers quickly? By the way, he did ask the captain how large the numbers inside the square root could get, and the captain replied

"one billion". He was probably exaggerating, but you’d better make sure the program works.

If you can successfully help the pirates, you’ll get a share of the treasure. It might be gold, it might be silver, or it might even be… a treasure map!

Input

The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

1.One line with one integer N, satisfying 1 <= N <= 10^9.

1.One line with one integer N, satisfying 1 <= N <= 10^9.

Output

For every test case in the input, the output should contain two nonnegative integers, separated by a space, on a single line: the distance pirate A needs to head in the perpendicular direction, and the final distance between pirate

A and B, such that pirate B has traveled sqrt(N) steps. If there are multiple solutions, give the one with the smallest numbers. If there are no solutions, the output should be "IMPOSSIBLE" (without the quotation marks) on a single line.

A and B, such that pirate B has traveled sqrt(N) steps. If there are multiple solutions, give the one with the smallest numbers. If there are no solutions, the output should be "IMPOSSIBLE" (without the quotation marks) on a single line.

Sample Input

4 33 16 50 101

Sample Output

4 7 0 4 IMPOSSIBLE 50 51

Source

Recommend

lcy

题意 ：输入n

是否能找到2个最小的x y 使得 x*x-y*y=n

思路 （x-y）*(x+y)=n

a=x–y b=x+y

a*b=n

暴力a 求b 满足条件即可

#include<stdio.h> #include<math.h> int main() { int cas,m,n,x,y,a,b,flag; scanf("%d",&cas); while(cas--) { flag=0; scanf("%d",&n); m=(int)sqrt(n*1.0); for(a=m;a>=1;a--) { if(n%a==0) { b=n/a; if((b-a)%2==0&&(a+b)%2==0) { flag=1; x=(a+b)/2; y=(b-a)/2; printf("%d %d\n",y,x); break; } } } if(flag==0) printf("IMPOSSIBLE\n"); } return 0; }

版权声明：本文为博主原创文章，未经博主允许不得转载。