B.牛牛变魔术 原题连接
模拟即可。
//显然有以下几种情况分类讨论:
1.a==target||b==target
2.target%2==1,因为每次操作后要翻倍,所以目标值是奇数显然无解
3.a+b<target
4.a+b>target
以上四种情况即可讨论完所有问题空间。
//显然有以下几种情况分类讨论:
1.a==target||b==target
2.target%2==1,因为每次操作后要翻倍,所以目标值是奇数显然无解
3.a+b<target
4.a+b>target
以上四种情况即可讨论完所有问题空间。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,target;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>a>>b>>target;
if(a==target||b==target)
{
cout<<0<<endl;
continue;
}
if(target%2==1)
{
cout<<"-1\n";
continue;
}
else
{
if(a+b<target)
{
ll num = a+b;
ll cnt = 0;
while(a+b<target)
{
a*=2,b*=2;
cnt++;
if(a+b>=target)
{
break;
}
}
cout<<cnt<<endl;
continue;
}
else
{//a+b>=target
cout<<"1\n";
continue;
}
}
}
}