题目链接:csoj | J. Rikka with Square Numbers (scnu.edu.cn)
题目解析:
代码如下:
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T--)
{
int a, b;
cin >> a >> b;
if (a < b) swap(a, b);
int d = a - b;
int tm = (int)sqrt(d);
if (tm * tm == d)
{
cout << "1" << endl;
continue;
}
if (d % 2 != 0 || d % 4 == 0) cout << "2" << endl;
else
{
bool flag = false;
for (int i = 1; i <= d / i; i++)
{
int t = d - (i * i);
tm = (int)sqrt(t);
if (tm * tm == t)
{
flag = true;
break;
}
}
if (flag) cout << "2" << endl;
else cout << "3" << endl;
}
}
return 0;
}