ZOJ 2705 Dividing a Chocolate

Description

The boy and Karlsson are dividing a chocolate that the boy's parents have presented him for his birthday. A chocolate is a rectangle that consists of m × n chocolate squares, separated from each other by cutlines.

Of course, Karlsson does the dividing. First Karlsson divides the chocolate into two rectangular pieces, breaking it along some cutline. Since Karlsson wants to divide chocolate fairly, he would not be satisfied if the two pieces have different sizes. In this case he cuts away the piece equal to the smaller piece out of the larger piece, and eats it. If the pieces are still not equal, he does so again, and so on. After the pieces are finally equal, Karlsson eats one of the pieces, and the boy eats another.

Karlsson wants to make an initial cut in such a way that he would eat as much chocolate as possible. However, the boy knows that Karlsson likes chocolate very much, and he watches for the process closely. Karlsson must be very careful not to offend the boy. So he must not successively cut away and eat chocolate from the same piece --- this would seem too suspicious to the boy.

Help Karlsson to find out how much chocolate he can eat.

Input

There are mutiple cases in the input file.

Each case contains m and n (1 <= m, n <= 109 ).

There is an empty line after each case.

Output

Output one integer number --- how many chocolate squares Karlsson can eat. If Karlsson cannot divide chocolate using his method, output 0 instead.

There should be am empty line after each case.

Sample Input

6 5

Sample Output

24

In the example Karlsson must initially break a chocolate into pieces of sizes 3 × 6 and 2 × 6 . After that he can eat all chocolate but the boy's piece that would finally be 1 × 6 . Karlsson cannot, for example, cut a chocolate initially into pieces of sizes 5 × 5 and 1 × 5 --- after doing so, he would have to successively cut a piece away from the first one several times, that would offend the boy.


斐波那契数列的应用

#include<cstdio>
#include<vector>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=105;
typedef long long ll;
ll n,m,f[maxn];

void pre()
{
	f[0]=f[1]=1;
	for (int i=2;i<maxn;i++) f[i]=f[i-1]+f[i-2];
}

ll work(ll x,ll y)
{
	ll ans;
	for (int i=50;i;i--)
		if (x%f[i]==0) {ans=f[i]; break;}
	return (x-x/ans)*y;
}

int main()
{
	pre();
	while (cin>>n>>m) 
	{
		cout<<max(work(n,m),work(m,n))<<endl<<endl;	
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值