A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

 A. Integer Moves

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There's a chip in the point (0,0) of the coordinate plane. In one operation, you can move the chip from some point (x1,y1) to some point (x2,y2) if the Euclidean distance between these two points is an integer (i.e. (x1x2)2+(y1y2)2 is integer).

Your task is to determine the minimum number of operations required to move the chip from the point (0,0) to the point (x,y).

Input

The first line contains a single integer t (1t3000) — number of test cases.

The single line of each test case contains two integers x and y (0x,y50) — the coordinates of the destination point.

Output

For each test case, print one integer — the minimum number of operations required to move the chip from the point (0,0) to the point (x,y).

Example
input
Copy
3
8 6
0 0
9 15
output
Copy
1
0
2
Note

In the first example, one operation (0,0)(8,6) is enough. (08)2+(06)2=64+36=100=10 is an integer.

In the second example, the chip is already at the destination point.

In the third example, the chip can be moved as follows: (0,0)(5,12)(9,15)(05)2+(012)2=25+144=169=13 and (59)2+(1215)2=16+9=25=5 are integers.



solution

#include<iostream>
#include<cmath>
using namespace std;

void solve()
{
	int x, y;
	double ans;
	cin >> x >> y;
	ans = sqrt(pow(0 - x, 2) + pow(0 - y, 2));
	if (x == 0 && y == 0)
		cout << 0 << "\n";
	else if (ans == (int)ans)
		cout << 1 << "\n";
	else
		cout << 2 << "\n";
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

No comments:

Post a Comment