Constructing a Square Root
Time limit1sMemory limit128 MB
For each N up to 1e9, find non-negative integers a and r with r^2 - a^2 = N and minimal a, or report IMPOSSIBLE.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Brute force
- Solved
- No attempts yet
Problem
A classic straightedge-and-compass construction uses only an unmarked straightedge and a compass. Because a compass is available, any integer multiple of the unit length (length ) is easy to construct, and we can go further and construct the square root of any natural number.
For example, suppose we want to construct a segment of length . First pick a point on a horizontal line and construct a segment of length that is perpendicular to the line and has as one endpoint. Call the other endpoint . Now draw a circle of radius centered at ; if is one of the points where this circle meets the horizontal line, then by the Pythagorean theorem the segment has length .
Using this method we want to construct a segment of length . That is, we must choose a non-negative integer segment length and a non-negative integer circle radius such that . If several pairs work, use the one with the smallest segment length . (The segment length may be .)
Input
The first line contains the number of test cases . Each of the next lines contains one integer ().
Output
For each test case, print on one line two non-negative integers: the segment length and the circle radius , separated by a space. If several pairs satisfy the condition, print the one with the smallest segment length . If no such pair exists, print IMPOSSIBLE.