CF490D Chocolate(思维)

题目链接
题目描述
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a_{1}×b_{1} a
1

×b
1

segments large and the second one is a_{2}×b_{2} a
2

×b
2

segments large.

Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus’s mind and Paraskevi’s beauty are equally matched, so the two bars must have the same number of squares.

To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:

he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar,
or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.
In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.

Both variants aren’t always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16×23 16×23 , then Polycarpus can chip off a half, but not a third. If the bar is 20×18 20×18 , then Polycarpus can chip off both a half and a third. If the bar is 5×7 5×7 , then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.

输入格式
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a_{1}×b_{1} a
1

×b
1

segments large and the second one is a_{2}×b_{2} a
2

×b
2

segments large.

Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus’s mind and Paraskevi’s beauty are equally matched, so the two bars must have the same number of squares.

To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:

he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar,
or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.
In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.

Both variants aren’t always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16×23 16×23 , then Polycarpus can chip off a half, but not a third. If the bar is 20×18 20×18 , then Polycarpus can chip off both a half and a third. If the bar is 5×7 5×7 , then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.

输出格式
In the first line print m m — the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.

If there is no solution, print a single line with integer -1.

题意翻译
现在有两个块巧克力一块大小是 a_1\times b_1a
1

×b
1

的,另外一块大小是 a_2\times b_2a
2

×b
2

的。

现在要把两块巧克力变成面积一样大小,可以使用下列两种方法:

可以沿横向或纵向的网格线分成两等分,然后吃掉其中的一份。

可以沿横向或纵向的网格线分成 \dfrac23,\dfrac13
3
2

,
3
1

的两份,吃掉小的那一份。

因此使用第一种方法会留一半巧克力,用第二种方法会留下 \dfrac23
3
2

巧克力。

两种方法并不总是可行的,有些时候两种方法都不能再用了。比如巧克力大小是 16\times 2316×23 的时候,可以使用第一种方法,但是不能使用第二种方法。当大小是 20\times 1820×18的时候,可以使用第一种方法或者第二种方法。如果大小是 5\times 75×7 的时候,两种方法都不能使用。

问最少要操作几次才能使得两块巧克力的面积是一样的,并输出巧克力可能的大小。

Translated by Eason_AC
2020.11.18

输入输出样例
输入 #1 复制
2 6
2 3
输出 #1 复制
1
1 6
2 3
输入 #2 复制
36 5
10 16
输出 #2 复制
3
16 5
5 16
输入 #3 复制
3 5
2 1
输出 #3 复制
-1

分析:

令 c1 = a1 * b1, c2 = a2 * b2,c1经过若干次除2和若干次除3乘2,c2经过若干次除2和若干次除3乘2,最终c1等于c2。
所以现将c1和c2去掉因数2和3,得到c1’和c2’,如果c1’不等于c2’,就输出-1。
然后将c1和c2的2和3的因数的个数处理成一样的就可以了。处理3的时候,要除3乘2.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+10;
ll ff(ll p,int &x,int &y)//将p去掉2和3的因数 
{
	while(p%2==0)
	{
		x++;
		p=p/2;
	}
	while(p%3==0)
	{
		y++;
		p=p/3;
	}
	return p;
}
void ff1(ll &a,ll &b,int k) 
{
	while(a%3==0&&k>0)
	{
		a=a/3*2;
		k--;
	}
	while(b%3==0&&k>0)
	{
		b=b/3*2;
		k--;
	}
}
void ff2(ll &a,ll &b,int k)
{
	while(a%2==0&&k>0)
	{
		a=a/2;
		k--;
	}
	while(b%2==0&&k>0)
	{
		b=b/2;
		k--;
	}
}
int main()
{
	ll a1,b1,a2,b2;
	scanf("%lld%lld%lld%lld",&a1,&b1,&a2,&b2);
	ll c1=a1*b1;
	ll c2=a2*b2;
	int x1=0,x2=0,y1=0,y2=0;
	ll k1=ff(c1,x1,y1);
	ll k2=ff(c2,x2,y2); 
	if(k1!=k2) printf("-1");//表示c1和c2除2和3之外有不同的因数 
	else
	{
		int re=0;
		if(y1<y2)//使c1和c2因数3的个数一样 
		{
			re+=y2-y1;
			ff1(a2,b2,y2-y1);
			x2+=y2-y1;
		}
		else if(y1>y2)
		{
			re+=y1-y2;
			ff1(a1,b1,y1-y2);
			x1+=y1-y2;
		}
		if(x1<x2)//使c1和c2因数2的个数一样  
		{
			re+=x2-x1;
			ff2(a2,b2,x2-x1);
		}
		else if(x1>x2)
		{
			re+=x1-x2;
			ff2(a1,b1,x1-x2);
		}
		printf("%d\n",re);
		printf("%lld %lld\n",a1,b1);
		printf("%lld %lld",a2,b2);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值