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 ...
随机推荐
- poj2752Seek the Name, Seek the Fame(next数组)
题目传送门 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2439 ...
- java并发编程之美-阅读记录4
java并发包中的原子操作类,这些类都是基于非阻塞算法CAS实现的. 4.1原子变量操作类 AtomicInteger/AtomicLong/AtomicBoolean等原子操作类 AtomicLon ...
- SQL数据库—<6>存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- Flask-Login的实现
Flask-Login Flask-Login 为 Flask 提供用户 session 的管理机制.它可以处理 Login.Logout 和 session 等服务. 作用: 将用户的 id 储存在 ...
- ARM-LINUX学习记录
1:调用C语言函数之前会有一段汇编代码在前面执行来完成软硬件方面的初始化.比如:关闭看门狗:初始化时钟:设置堆栈:调用main函数等.在学习51单片机时候这些操作是由开发环境(如KEIL)在编译C代码 ...
- windows server 进入组策略管理
方法: win+R 然后输入 gpmc.msc 即可在域服务器上进行组策略管理了.
- 第8篇NFS PersistentVolume
一.部署nfs服务端: k8s-master 节点上搭建了 NFS 服务器,也可以在部署节点搭建,原理一样 (1)安装nfs服务: yum install -y nfs-utils rpcbind v ...
- eclipse maven install后查看报错信息
- js判断元素是否可见
dom元素是否可见可使用jq的is方法和dom的offsetParent === null方法 jq中 $(element).is(":visible") === true !!( ...
- Eigen ,MKL和 matlab 矩阵乘法速度比较
Eigen 矩阵乘法的速度 < MKL矩阵乘法的速度,MKL矩阵乘法的速度与matlab矩阵乘法的速度相差不大,但matlab GPU版本的矩阵乘法速度是CUP的两倍,在采用float数据类型 ...