思路:贪心+大胆猜想+推理,首先判断是否合法,第i盘得2*i-1,即为1,3,5,7,9...,2*i-1 这样两者分数和为 i*2,因此当a+b不是平方数时则不合法,而当a=2或b=2也不合法(全部分数只有一个1)
然后对a进行推理,由于相邻盘得分相差2,因此小的分数可以转换为大的分数,即 {2i-1,2j-1}可以转换为 {2(i-1)-1,2(j+1)+1},例如{3,5}可以转换为{1,7}, 而对于{1,3,7}不可转换为{1,1,9}都是可以看做后者
则一共i盘游戏,首先判断是否合法,在求出最大盘得分s=i*2-1,将s与a比较,若a>s则 a-=s,同时s-=2,ans++,直到a<=s,若a为奇数,则一定可以一盘得分,ans+=1,若a为偶数,则必须要两盘{1,a-1},ans+=2,其对于{1,3,7}同样成立
Code :
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
int T;
LL a,b;
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>a>>b;
LL s=sqrt(a+b);
int ans=0;
if(s*s==a+b&&a!=2&&b!=2){
s=s*2-1;
while(a){
if(s>a){
if(a%2==0) ans+=2;
else ans+=1;
break;
}else{
a-=s;
s-=2;
ans++;
}
}
}else ans=-1;
cout<<ans<<endl;
}
return 0;
}