Codeforces 490D Chocolate
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 is5 × 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 给两个巧克力。 大小分别是 a1 * b1 , a2 * b2 ;
然后可以横竖切走二分之一 , 三分之一 , 这样。
问能否通过最小步数使得两块巧克力的面积一样 。 我的做法很恶心。
直接将4个数分解。
得出各条边能够通过切割形成的边长 ( 用set去掉重复的 ),并且记录切割次数 。 发现边集规模不大 。
然后我就暴力构造出前两个边集能够形成的矩阵。
用后两个边集也构出矩阵集合。
然后再枚举第一个矩阵集合 e[0][i] , 在第二个矩阵集合中进行二分, 看看是否有矩阵跟 e[0][i] 的面积相同 ,有就取一个切割次数最少的 。
然后再更新一下答案这样子。 貌似正解是分解完数后 , 通过一些计算即可得出答案 。 没必要如此暴力~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std; typedef long long LL;
const int N = ;
const int inf = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ; set<LL>num_cnt; struct node{
LL x , y , area , cnt ;
bool operator < ( const node &a ) const{
if( area != a.area ) return area < a.area ;
else return cnt < a.cnt ;
}
}e[][N]; LL a[] , num[][N] , cnt[][N] , tot[] , tail1 , tail2 ; void cut( LL n , int i , int cnts ) {
if( num_cnt.count(n) > ) return ; num_cnt.insert(n);
num[i][ tot[i] ] = n , cnt[i][tot[i]] = cnts ; tot[i]++;
if( n % == ) cut( n / , i , cnts + );
if( n % == ) cut( n / * , i , cnts + );
} void test() {
for( int i = ; i < tail1 ; i ++ ){ cout << e[][i].x <<' ' << e[][i].y <<' '<< e[][i].area << endl ; } cout << endl ;
for( int i = ; i < tail2 ; i ++ ){ cout << e[][i].x <<' ' << e[][i].y <<' '<< e[][i].area << endl ; } cout << endl ;
} void solve() { int temp1 = , temp2 = ;
for( int i = ; i < tail1 ; i ++ ){
if( e[][i].area == e[][ temp1 - ].area ) continue ;
e[][temp1++] = e[][i] ;
}
for( int i = ; i < tail2 ; i ++ ){
if( e[][i].area == e[][ temp2 - ].area ) continue ;
e[][temp2++] = e[][i] ;
}
tail1 = temp1 , tail2 = temp2 ;
} int find( LL area )
{
int l = , r = tail2 ;
if( e[][l].area == area ) return l ;
else if( e[][r].area == area ) return r ;
while( l <= r ){
int mid = ( l + r ) >> ;
if( e[][mid].area == area ){
return mid ;
}
else if( e[][mid].area < area )
l = mid + ;
else
r = mid - ;
}
return - ;
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
for( int i = ; i < ; ++i ) {
cin >> a[i] ; num_cnt.clear();
cut( a[i] , i , );
}
// for( int i = 0 ; i < 4 ; ++i ) cout << tot[i] << endl ;
tail1 = , tail2 = ;
for( int i = ; i < tot[] ; ++i ){
for( int j = ; j < tot[] ; ++j ) {
e[][ tail1 ].x = num[][i] ;
e[][ tail1 ].y = num[][j] ;
e[][ tail1 ].area = num[][i] * num[][j] ;
e[][ tail1 ].cnt = cnt[][i] + cnt[][j];
tail1++ ;
}
} for( int i = ; i < tot[] ; ++i ){
for( int j = ; j < tot[] ; ++j ) {
e[][ tail2 ].x = num[][i] ;
e[][ tail2 ].y = num[][j] ;
e[][ tail2 ].area = num[][i] * num[][j] ;
e[][ tail2 ].cnt = cnt[][i] + cnt[][j];
tail2++ ;
}
} sort( e[] , e[] + tail1 ) ; sort( e[] , e[] + tail2 ) ; solve(); LL ans = inf , A1 , A0 , B1 , B0 ; for( int i = ; i < tail1 ; ++i ){
if( i > && e[][i].area == e[][i-].area ) continue ;
if( ans <= e[][i].cnt ) continue ;
int pos = find( e[][i].area );
if( pos == - ) continue ;
if( ans > e[][i].cnt + e[][pos].cnt ){
ans = e[][i].cnt + e[][pos].cnt ;
A0 = e[][i].x , B0 = e[][i].y;
A1 = e[][pos].x , B1 = e[][pos].y;
}
}
if( ans != inf ) cout << ans << '\n' << A0 << ' ' << B0 << '\n' << A1 <<' ' << B1 << endl ;
else cout << "-1" << endl ;
}
Codeforces 490D Chocolate的更多相关文章
- codeforces 617B Chocolate
题意: 在给定01串中,问能分割成多少个子串?每个子串只有一个1. dp #include<iostream> #include<string> #include<alg ...
- CodeForces 598E Chocolate Bar
区间DP预处理. dp[i][j][k]表示大小为i*j的巧克力块,切出k块的最小代价. #include<cstdio> #include<cstring> #include ...
- 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/ ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- Educational Codeforces Round 1 E. Chocolate Bar dp
题目链接:http://codeforces.com/contest/598/problem/E E. Chocolate Bar time limit per test 2 seconds memo ...
- Codeforces 689C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves time limit per test:2 seconds memory limit per test:256 megabytes inpu ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
随机推荐
- Swift——(六)Swift中的值类型
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/34855597 在Swift中,结构体和枚举 ...
- 字符串格式的Url的截取
一,我们先在看在页面上获取的URL的处理,如下方法: //获取全部URL string Url = Request.Url.ToString(); Url += "</br>&q ...
- echarts学习思路及常用属性记录
此篇博文分享自己对于入门学习echart的思路及对常见组件的用法记录,如serise.data和坐标轴对应关系,多个坐标轴,多个grid的对齐,tooltip的超出处理,坐标轴/toolti ...
- SQL数据库—<6>存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- XMPP即时通讯协议使用(十二)——基于xmpp搭建简单的局域网WebRTC
创建HTML和JS ofwebrtc.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...
- 一 shell编程
好啦.从今天开始我们转入shell编程的行列.从鸟哥私房菜中,已经学到了一些shell编程的皮毛,这两个月打算系统的学习,学会,学熟练.加油吧 bash shell [root@localhost s ...
- 2019CCPC网络赛 HDU6705 - path K短路
题意:给出n个点m条边的有向图,问图上第K短路的长度是多少(这里的路可以经过任何重复点重复边). 解法:解法参考https://blog.csdn.net/Ratina/article/details ...
- css3--css3模块
CSS3被拆分为"模块".旧规范已拆分成小块,还增加了新的. 一些最重要CSS3模块如下: 1.选择器(.基本选择器,层次选择器,伪类选择器,伪元素,属性选择器,通配符) 2.盒模 ...
- c++简单线程池实现(转)
线程池,简单来说就是有一堆已经创建好的线程(最大数目一定),初始时他们都处于空闲状态,当有新的任务进来,从线程池中取出一个空闲的线程处理任务,然后当任务处理完成之后,该线程被重新放回到线程池中,供其他 ...
- vue video全屏播放
需求: 1.视频为长方形,页面初始化打开为横屏全屏播放视频. 2.微信不支持自动播放,故自动播放需求删除. 方法: 1.vue-video-player插件 因需求较简单,仅要求播放本地一个视频,故未 ...