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 ...
随机推荐
- HDU 6470 /// 矩阵快速幂
题目大意: f[1]=1 f[2]=2 f[n]=f[n-1]+2*f[n-2]+n^3 在某博客截的图 现在忘记原博位置了 抱歉 根据递推式1和递推式3构造出两个矩阵 #include <bi ...
- RecyclerView跳转到指定位置的三种方式
自从android5.0推出RecyclerView以后,RecyclerView越来越受广大程序员的热爱了!大家都知道RecyclerView的出现目的是为了替代listview和ScrollVie ...
- jQuery实现网页放大镜功能 转载
京东等电商网站中可以对商品进行放大观察,本文要实现的就是模仿这个放大镜功能,大致效果如下图所示: 简要说明实现思路: 1.原图窗口与放大窗口插入的是同一个图片,不过原图窗口的图片要适当缩小,放大窗口图 ...
- jsp获取url路径的方法
如果你请求的URL是 http://localhost:8080/demo/Index.jsp request.getScheme() //输出:http request.getServerName ...
- mysql添加中文数据失败
日志信息: INFO 2015-01-13 10:44:36,078 org.springframework.beans.factory.xml.XmlBeanDefinitionReader: Lo ...
- 服务器中卸载JDK
- 页面重置样式reset.css
我把经常用到的一些页面重置样式归类到了一个.css文件中,这样可以减少代码冗余.当然还有其他的很多用处,比如h1~h5的样式全部统一的话,下面写东西很清晰很多. @charset 'utf-8'; h ...
- PHP curl_reset函数
curl_reset— 重置libcurl会话句柄的所有选项. 说明 void curl_reset ( resource $ch ) 该函数将重新初始化cURL的所有选项值(默认值). 注意:cur ...
- PHP opendir() 函数
打开一个目录,读取它的内容,然后关闭: <?php$dir = "/images/"; // Open a directory, and read its contentsi ...
- 【dart学习】之字典(Map)的相关方法总结
一,概述 通常来讲,Map是一个键值对相关的对象,键和值可以是任何类型的对象.每个键只出现一次,而一个值则可以出现多次.映射是动态集合. 换句话说,Maps可以在运行时增长和缩小. dart:core ...