Codeforces Round #279 (Div. 2) D. Chocolate

本文介绍了一种通过操作矩形的尺寸来使两个矩形面积相等的算法。该算法首先计算矩形面积中2和3因子的数量,然后通过转换因子来平衡两个矩形的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: LINK

题意:给定两个矩形a1*b1 和 a2*b2,每次操作是选择一个矩形对它水平或者垂直的 * 1/2, 或者 * 2/3必须整除,不能整除就无法进行。
要求最少的操作使得操作完的两个矩形的面积大小一样。


可以求出a1*b1 和 a2*b2 分别有多少个因子2, 和多少个因子3,除去这些因子剩下的部分这两者应该一样才可以,不一样的话NO。
之后如果一个数*2/3就相当于把它的因子3转换成2,如果*1/2就是消去一个因子2.
我们就把因子3多的那个数(*2/3)消去多余的因子3 同时生成新的因子2,之后两者因子3的个数一样多,之后就是(*1/2)去掉因子2多的那个数中多余的因子2,就可以了.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; 
#define INF 1000000000
typedef __int64 LL; 
LL two1, three1, two2, three2; 
LL a1, b1, a2, b2; 
LL gao(LL ans, LL &two, LL &three) {
	two = 0; 
	while(ans % 2 == 0) {
		two ++; 
		ans /= 2; 
	}
	three = 0; 
	while(ans % 3 == 0) {
		three ++; 
		ans /= 3; 
	}
	return ans; 
}
void sol(LL &a, LL &b, LL del2, LL del3) {
	while(a%3 == 0 && del3 != 0) {
		a /= 3; del3--; 
		a *= 2; 
	}
	while(b%3 == 0 && del3 != 0) {
		b /= 3; del3--; 
		b *= 2; 
	}
	while(a % 2 == 0 && del2 != 0) {
		a /= 2; del2 --; 
	}
	while(b %2 == 0 && del2 != 0) {
		b /= 2; del2--; 
	}
}
int main() {
	cin>>a1>>b1>>a2>>b2; 
	LL ans1 = a1 * b1; 
	LL ans2 = a2 * b2; 
	LL g1 = gao(ans1, two1, three1); 
	LL g2 = gao(ans2, two2, three2); 
	if(g1 != g2) {
		puts("-1"); return 0; 
	}
	LL ans = 0; 
	LL del13, del12, del23, del22; 
	del13 = del12 = del23 = del22 = 0; 
	ans += abs(three1 - three2); 
	if(three1 > three2) {
		del13 = ans; two1 += ans; 
	}
	else if(three1 < three2) {
		del23 = ans; two2 += ans; 
	}
	ans += abs(two1 - two2); 
	if(two1 > two2) del12 = two1 - two2; 
	else del22 = two2 - two1; 
	cout<<ans<<endl; 
	sol(a1, b1, del12, del13); 
	sol(a2, b2, del22, del23); 
	cout<<a1<<" "<<b1<<endl; 
	cout<<a2<<" "<<b2<<endl; 
	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值