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]);
- }
1 second
256 megabytes
standard input
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.
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).
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.
- 2 6
- 2 3
- 1
- 1 6
- 2 3
- 36 5
- 10 16
- 3
- 16 5
- 5 16
- 3 5
- 2 1
- -1
codeforces 490 D Chocolate的更多相关文章
- Codeforces Problem 598E - Chocolate Bar
Chocolate Bar 题意: 有一个n*m(1<= n,m<=30)的矩形巧克力,每次能横向或者是纵向切,且每次切的花费为所切边长的平方,问你最后得到k个单位巧克力( k <= ...
- Codeforces 633F The Chocolate Spree 树形dp
The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...
- Codeforces 617B:Chocolate(思维)
题目链接http://codeforces.com/problemset/problem/617/B 题意 有一个数组,数组中的元素均为0或1 .要求将这个数组分成一些区间,每个区间中的1的个数均为1 ...
- codeforces 598E E. Chocolate Bar(区间dp)
题目链接: E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 633F The Chocolate Spree (树形dp)
题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...
- Codeforces 598E:Chocolate Bar
E. Chocolate Bar time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 633F - The Chocolate Spree(树形 dp)
Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...
- Codeforces Round #340 (Div. 2) B. Chocolate 水题
B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...
- Codeforces Round #310 (Div. 1) C. Case of Chocolate set
C. Case of Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/ ...
随机推荐
- 常见Java集合的实现细节
1. Set和Map Set代表一种集合元素无序.集合元素不可重复的集合,Map则代表一种由多个key-value对组成的集合,Map集合类似于传统的关联数组.表面上看它们之间相似性很少,但实际上Ma ...
- Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学
题面 题意:给你n,m,k,在你在(0,0)到(n,m)的矩形内,选3个格点(x,y都是整数),使得三角形面积为n*m/k,不能找到则输出-1 题解:由毕克定理知道,格点多边形的面积必为1/2的整数倍 ...
- 使用WebGL + Three.js制作动画场景
使用WebGL + Three.js制作动画场景 3D图像,技术,打造产品,还有互联网:这些只是我爱好的一小部分. 现在,感谢WebGL的出现-一个新的JavaScriptAPI,它可以在不依赖任何插 ...
- spring IOC(DI)和AOP
软件152谭智馗 IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. DI—Dependency Injecti ...
- Linker scripts之SECTIONS
1 Purpose The linker script describes how the sections in the input files should be mapped into the ...
- OpenCV中GPU模块使用
CUDA IT168的文章系列: Cuda的初始化:http://tech.it168.com/a2011/0715/1218/000001218458.shtml OpenCV: OpenCV中GP ...
- PCL:解决PCL和OpenCV冲突的方法
不是PCL的问题,而是OpenCV的问题. (1):先包含PCL库,再包含OpenCV库: (2):把里面的UCHAR冲突全部换掉! 如果你有闲情逸致,用正则表达式 慢慢替换去吧! (3):或者把F ...
- 我的C++笔记(函数部分)
#include <iostream> #include <cmath>//C++的数学函数库 using namespace std; class Point{ public ...
- 引用css样式的书写格式
css的书写格式一共有三种 行内样式:意思是在行内中写样式 例如说<p style="color:red">用行内样式编写我的颜色</p> 只适用于< ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--MVC控制器的设计
http://www.cnblogs.com/wuhuacong/p/3284628.html 自从上篇<基于MVC4+EasyUI的Web开发框架形成之旅--总体介绍>总体性的概括,得到 ...