题面
题目描述
输入
输出
样例输入
2
2 2 2
4 6 8
样例输出
-1
1
数据范围
题解
思路1
暴力!!!
没错,就是暴力。
我们先看一下暴力的操作。
是三个数原来分别为
a
,
b
,
c
a,b,c
a,b,c,则进行一次操作后三个数变为
b
2
+
c
2
,
a
2
+
c
2
,
a
2
+
b
2
\frac{b}{2}+\frac{c}{2},\frac{a}{2}+\frac{c}{2},\frac{a}{2}+\frac{b}{2}
2b+2c,2a+2c,2a+2b
可证明其操作次数一定在63以内虽然我不会证明 。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int i,j,n,m,k,l,o,p;
struct node
{
int a,b,c;
}x;
inline void cl(node &z)
{
node y=z;
z.a=(y.b+y.c)/2;
z.b=(y.a+y.c)/2;
z.c=(y.a+y.b)/2;
}
int main()
{
freopen("carry.in", "r", stdin);
freopen("carry.out", "w", stdout);
scanf("%d",&n);
while (n--)
{
scanf("%d %d %d",&x.a,&x.b,&x.c);
node q=x;
int tim=0;
if (x.a==x.b&&x.b==x.c) {printf("-1\n");continue;}
while (((x.a&1)==0)&&((x.b&1)==0)&&((x.c&1)==0))
{
tim++;
cl(x);
}
else printf("-1\n");
}
}
暴力代码就不需要注释了吧。
思路2
考虑把三个数表示为 2 x 1 ⋅ y 1 , 2 x 2 ⋅ y 2 , 2 x 3 ⋅ y 3 2^{x_1} \cdot y_1,2^{x_2} \cdot y_2,2^{x_3} \cdot y_3 2x1⋅y1,2x2⋅y2,2x3⋅y3。那么,类似的,进行一次操作后我们可以得到对应的 2 x 2 − 1 ⋅ y 2 + 2 x 3 − 1 ⋅ y 3 , 2 x 1 − 1 ⋅ y 1 + 2 x 3 − 1 ⋅ y 3 , 2 x 1 − 1 ⋅ y 1 + 2 x 2 − 1 ⋅ y 2 2^{x_2-1} \cdot y_2+2^{x_3-1} \cdot y_3,2^{x_1-1} \cdot y_1+2^{x_3-1} \cdot y_3,2^{x_1-1} \cdot y_1+2^{x_2-1} \cdot y_2 2x2−1⋅y2+2x3−1⋅y3,2x1−1⋅y1+2x3−1⋅y3,2x1−1⋅y1+2x2−1⋅y2这三个数,显然可以发现,最终的操作次数为 x 1 , x 2 , x 3 x_1,x_2,x_3 x1,x2,x3中较小的那个数(错误思想)
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define min(x,y) ((x<y)?(x):(y))
int i,j,n,m,k,l,o,p;
struct node
{
long long a,b,c;
int xx,yy,zz;
}x;
inline void js()
{
x.xx=x.yy=x.zz=0;
while ((!(x.a&1))&&x.a)
{
x.a/=2,x.xx++;
}
if (x.xx==x.a&&x.xx==0) x.xx=12312;
while ((!(x.b&1))&&x.b)
{
x.b/=2,x.yy++;
}
if (x.yy==x.b&&x.yy==0) x.yy=12312;
while ((!(x.c&1))&&x.c)
{
x.c/=2,x.zz++;
}
if (x.zz==x.c&&x.zz==0) x.zz=12312;
}
int main()
{
freopen("carry.in", "r", stdin);
freopen("carry.out", "w", stdout);
scanf("%d",&n);
while (n--)
{
scanf("%lld %lld %lld",&x.a,&x.b,&x.c);
int tim=0;
if (x.a==x.b&&x.b==x.c) {printf("-1\n");continue;}
js();
printf("%d\n",min(min(x.xx,x.yy),x.zz));
}
}
不难发现其实这个思想是有问题的,因为若
x
1
=
=
x
2
=
=
x
3
x_1==x_2==x_3
x1==x2==x3,那么这三个奇数的和又有可能使二的次幂数增加。
举个例子,如2,6,10
2
=
2
1
⋅
1
,
6
=
2
1
⋅
3
,
10
=
2
1
⋅
5
2=2^1\cdot1,6=2^1\cdot3,10=2^1\cdot5
2=21⋅1,6=21⋅3,10=21⋅5
最高次幂数为1,那么答案应该为1。
但答案却是2
自己退一下便知
2,6,10
8,6,4
5,6,7结束操作
原因就是上面所讲到的。
思路3——对思路2的改进
简单粗暴,针对刚才的问题,既然当 x 1 = = x 2 = = x 3 x_1==x_2==x_3 x1==x2==x3时结果会少一,那我就给他补上一个一
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define min(x,y) ((x<y)?(x):(y))
int i,j,n,m,k,l,o,p;
struct node
{
long long a,b,c;
int xx,yy,zz;
}x;
inline void js()
{
x.xx=x.yy=x.zz=0;
while ((!(x.a&1))&&x.a)
{
x.a/=2,x.xx++;
}
if (x.xx==x.a&&x.xx==0) x.xx=12312;
while ((!(x.b&1))&&x.b)
{
x.b/=2,x.yy++;
}
if (x.yy==x.b&&x.yy==0) x.yy=12312;
while ((!(x.c&1))&&x.c)
{
x.c/=2,x.zz++;
}
if (x.zz==x.c&&x.zz==0) x.zz=12312;
}
int main()
{
freopen("carry.in", "r", stdin);
freopen("carry.out", "w", stdout);
scanf("%d",&n);
while (n--)
{
scanf("%lld %lld %lld",&x.a,&x.b,&x.c);
int tim=0;
if (x.a==x.b&&x.b==x.c) {printf("-1\n");continue;}
js();
printf("%d\n",min(min(x.xx,x.yy),x.zz)+(x.xx==x.yy&&x.yy==x.zz&&x.xx!=0));
}
}
思路4——思路2与思路1的结合
既然x中有两个相等时会出事,那我就先让这三个数一直进行操作直到不相等为止。
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define min(x,y) ((x<y)?(x):(y))
int i,j,n,m,k,l,o,p;
struct node
{
long long a,b,c;
int xx,yy,zz;
}x;
inline void hy()
{
x.a*=(long long)pow(2,x.xx);
x.b*=(long long)pow(2,x.yy);
x.c*=(long long)pow(2,x.zz);
}
inline void cl(node &z)
{
node y=z;
z.a=(y.b+y.c)/2;
z.b=(y.a+y.c)/2;
z.c=(y.a+y.b)/2;
}
inline void js()
{
x.xx=x.yy=x.zz=0;
while ((!(x.a&1))&&x.a)
{
x.a/=2,x.xx++;
}
while ((!(x.b&1))&&x.b)
{
x.b/=2,x.yy++;
}
while ((!(x.c&1))&&x.c)
{
x.c/=2,x.zz++;
}
}
int main()
{
freopen("carry.in", "r", stdin);
freopen("carry.out", "w", stdout);
scanf("%d",&n);
while (n--)
{
scanf("%lld %lld %lld",&x.a,&x.b,&x.c);
int tim=0;
if (x.a==x.b&&x.b==x.c) {printf("-1\n");continue;}
if (((x.a&1)==0)&&((x.b&1)==0)&&((x.c&1)==0)) js();
else
{
printf("%d\n",0);
continue;
}
if ((x.a==x.xx&&x.a==0)||(x.b==x.yy&&x.b==0)||(x.c==x.zz&&x.c==0))
{
tim++;
hy();
cl(x);
if (((x.a&1)==0)&&((x.b&1)==0)&&((x.c&1)==0)) js();
else
{
printf("%d\n",1);
continue;
}
}
if ((x.a==x.xx&&x.a==0)||(x.b==x.yy&&x.b==0)||(x.c==x.zz&&x.c==0))
{
tim++;
hy();
cl(x);
js();
}
while ((x.xx==x.yy&&x.xx!=0)||(x.yy==x.zz&&x.yy!=0)||(x.xx==x.zz&&x.xx!=0))
{
tim++;
hy();
cl(x);
js();
}
printf("%d\n",tim+min(min(x.xx,x.yy),x.zz));
}
}