codeforces 490 D Chocolate

题意:给出a1*b1和a2*b2两块巧克力,每次可以将这四个数中的随意一个数乘以1/2或者2/3,前提是要可以被2或者3整除,要求最小的次数让a1*b1=a2*b2,并求出这四个数最后的大小。

做法:非常显然仅仅跟2跟3有关。所以s1=a1*b1,s2=a2*b2,s1/=gcd(s1,s2),s2/=gcd(s1,s2),然后若s1跟s2的质因子都是2跟3,那么就有解。之后暴力乱搞就好了。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
	return b==0?a:gcd(b,a%b);
}
bool work(ll x,int *cnt)
{
	while(x%2==0)
	{
		x/=2;
		cnt[2]++;
	}
	while(x%3==0)
	{
		x/=3;
		cnt[3]++;
	}
	return x==1;
}
void cg(int &a,int &b,int val,int num)
{
	while(a%val==0&&num>0)
	{
		num--;
		a/=val;
		if(val==3)
			a*=2;
	}
	while(b%val==0&&num>0)
	{
		num--;
		b/=val;
		if(val==3)
			b*=2;
	}
}
int num[2][4];
int a[2],b[2];
ll s[2];
int main()
{
	for(int i=0;i<2;i++)
	{
		cin>>a[i]>>b[i];
		s[i]=ll(a[i])*b[i];
	}
	ll t=gcd(s[0],s[1]);
	s[0]/=t;s[1]/=t;
	if(!work(s[0],num[0])||!work(s[1],num[1]))
	{
		puts("-1");
		return 0;
	}
	int ans=0;
	for(int i=3;i>1;i--)
	{
		int sub=abs(num[0][i]-num[1][i]);
		ans+=sub;
		if(num[0][i]>num[1][i])
		{
			num[0][i-1]+=sub;
			cg(a[0],b[0],i,sub);
		}
		else
		{
			num[1][i-1]+=sub;
			cg(a[1],b[1],i,sub);
		}
	}
	printf("%d\n%d %d\n%d %d",ans,a[0],b[0],a[1],b[1]);
}

D. Chocolate

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 a1?×?b1 segments
large and the second one is a2?×?b2 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, then
Polycarpus can chip off a half, but not a third. If the bar is 20?×?18, then Polycarpus can chip off both a half and a third. If the bar is 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.

Input

The first line of the input contains integers a1,?b1 (1?≤?a1,?b1?≤?109)
— the initial sizes of the first chocolate bar. The second line of the input contains integers a2,?b2 (1?≤?a2,?b2?≤?109)
— the initial sizes of the second bar.

You can use the data of type int64 (in Pascal), long
long (in С++), long (in Java) to process large integers (exceeding 231?-?1).

Output

In the first line print 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 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.

Sample test(s)

input

2 6
2 3

output

1
1 6
2 3

input

36 5
10 16

output

3
16 5
5 16

input

3 5
2 1

output

-1
时间: 2024-12-17 21:26:24

codeforces 490 D Chocolate的相关文章

codeforces 598E E. Chocolate Bar(区间dp)

题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you ma

Codeforces 633F The Chocolate Spree 树形dp

The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #d

Codeforces 490D Chocolate(数论)

题目链接:Codeforces 490D Chocolate 两个种变换方式无疑是减掉一个因子3加上一个因子2和减掉一个因子2,所以从因子的角度出发,如果两组数存在不同的质因子肯定是不可以的.剩下的就是构造答案了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 105; int A

Codeforces Round #257 (Div. 2)449A - Jzzhu and Chocolate(贪心、数学)

题目链接:http://codeforces.com/problemset/problem/449/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

CodeForces Round 279 D 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 a1 × b1 segments large and the second one is a2 × b2 segments large. Polycarpus wants to give Paras

codeforces 555 C Case of Chocolate

一开始题目读错了,还以为可以从任意点为起点向上向左吃. 其实是只能从右边的边界为起点吃. 于是很明显,每一个横坐标最多只能出现一次,否则肯定是当前这个起点的巧克力已经被啃食了. 想到这里就更明显了,对于(xi,n+1-xi),若是向上吃,能够影响它的操作(xj,n+1-xj)肯定满足xj>xi,然后又明显一点,最小的xj肯定能影响到它. 我们来考虑操作(xj,n+1-xj), 若它是往左吃,很显然此时操作(xi,n+1-xi)能吃到被(xj,n+1-xj)吃掉的地方为止. 若它是往上吃呢?很显然

CodeForces 449A - Jzzhu and Chocolate

传送门:Jzzhu and Chocolate 题意: 给出一个N * M的矩阵,给K个操作,每次操作可以横/竖切割矩阵,最后求K次切割之后,矩阵最小的那块面积最大是多少? 分析: 按照题意,题目求的结果是:(最小的面积,最大是多少),那么可以想到,K次切割之后尽量使得每个块的面积相等,某些块比较大(因为不一定能平均分),那么就可以使得最小的那块面积最大了. 然后如何切割呢? 因为有横竖两个切割方法,那么我们可以设X为横切割数,Y为竖切割数,其中(0 <= X, Y <= K),因为最多对一个

Codeforces Round #490 (Div. 3) B

传送门http://codeforces.com/contest/999/problem/B 一个长度为n的字符串t,将n的因子从大到小排个序(设为a[i]),每次把t的第一个字符到第a[i]个字符reverse一下,最终得到一个怪异的串s.现在给n和s,求t. 最长100个字符,直接模拟好了.先把n分解质因数,然后进行"逆操作",即把因子从小到大排序,然后把给的串reverse回去. 我在做的时候忘记reverse怎么用了,尴尬. 1 #include <iostream>

Codeforces 450C:Jzzhu and Chocolate(贪心)

C. Jzzhu and Chocolate time limit per test: 1 seconds memory limit per test: 256 megabytes input: standard input output: standard output Jzzhu has a big rectangular chocolate bar that consists of \(n?×?m\) unit squares. He wants to cut this bar exact