JZOJ5376. 【NOIP2017提高A组模拟9.19】Candy

题面

题目描述

在这里插入图片描述

输入

在这里插入图片描述

输出

在这里插入图片描述

样例输入

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 2x1y1,2x2y2,2x3y3。那么,类似的,进行一次操作后我们可以得到对应的 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 2x21y2+2x31y3,2x11y1+2x31y3,2x11y1+2x21y2这三个数,显然可以发现,最终的操作次数为 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=211,6=213,10=215
最高次幂数为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));
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值