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 ...
随机推荐
- 44-python基础-python3-字符串-常用字符串方法(二)-isalpha()-isalnum()-isdigit()-isspace()-istitle()
3-isX 字符串方法 序号 方法 条件 返回结果1 返回结果2 1 isalpha() 如果字符串只包含字母,并且非空; True False 2 isalnum() 如果字符串只包含字母和数字 ...
- 容器下载的是centos8的镜像,scp出现packet_write_wait: Connection to **** port 22: Broken pipe 问题解决
解决方案:在~/.ssh目录新建文件config vi ~/.ssh/config #Added lines to fix. Host * IPQoS lowdelay t ...
- ASN.1
ASN.1抽象语法标记(Abstract Syntax Notation One https://baike.baidu.com/item/ASN.1/498523?fr=aladdin
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM(转:示例挺好,不太赞同画图)
转自:http://zjutkz.net/2016/04/13/%E9%80%89%E6%8B%A9%E6%81%90%E6%83%A7%E7%97%87%E7%9A%84%E7%A6%8F%E9%9 ...
- 【串线篇】SQL映射文件EmployeeDao.xml事项
Dao.xml或者说是mapper.xml一个意思 id:方法名,相当于这个配置是对于某个方法的实现 ,参数类型不用写,()也不用写 resultType:指定方法运行后的返回值类型全类名:(查询操作 ...
- Python3.5-20190508-廖老师-自我笔记-迭代器
可以实现for ...in 的都可以称为,可迭代对象,Iterable 可以使用isinstance(是什么实例嘛)来判断是不是可迭代对象 可以被next()函数调用并不断返回下一个值的对象称为迭代器 ...
- c++后台开发面试常见知识点总结(四)数据库
数据库的索引类型 聚集索引和非聚集索引的区别(叶节点存储内容) 唯一性索引和主码索引的区别 索引的优缺点,什么时候使用索引,什么时候不能使用索引(重点) 索引最左前缀问题 数据库中事务的ACID 数据 ...
- Qt 【widget如何铺满窗口】
刚接触qt不是很长时间,都是使用ui拖拽控件实现界面,然后发现有些问题就是控件一旦多了起来,拖拽就不好控制了,然后就转而使用纯代码开发. 一下是碰到第一个问题: 创建一个MainWidget; Mai ...
- c# 7.0新语法特性
public class NewAtturibute { public void TestFunc() { // 01 Out变量 不用初始化 "; if (int.TryParse(inp ...
- 【纪中集训】2019.08.10【省选组】模拟TJ
前言 一套码农题-- T1 Description 给定一棵\(n(\in[2,10^5])\)个点的树,\(m(≤10^5)\)次询问,每次询问有两个不相同的点,要让所有点走到这两个点之一(走一条边 ...