https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1390
先看x+y是不是a1=1 d=2这个等差数列的前n项和 直接枚举项数
找到的话再枚举k(如果d=1那必然存在 但d>1且以1开头就未必) 看x是否介于前k项和后k项之间 注意判奇偶
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll getn(ll n)
{
return n*n;
}
int main()
{
ll x,y;
int t,i,p,ans;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&x,&y);
p=-1;
for(i=0;i<=2e6;i++)
{
if(getn(i)==x+y)
{
p=i;
break;
}
}
if(p!=-1)
{
ans=-1;
for(i=x%2;i<=p;i+=2)
{
if(getn(i)<=x&&x<=getn(p)-getn(p-i))
{
ans=i;
break;
}
}
printf("%d\n",ans);
}
else printf("-1\n");
}
return 0;
}