ZOJ_2705_Dividing a Chocolate(递推斐波那契)

Dividing a Chocolate
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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.

题型:递推


题意:

        boy有块n*m的巧克力让Karlsson来分,先沿着线给巧克力来一刀,分成两个矩形。如果这两个矩形不全等,那么在较大的巧克力上切出一块与较小的巧克力全等的部分并且将它吃掉;然后如果剩下来的两块不全等,则依次类推;若全等,那么剩下来的两块一块被karlsson吃掉,一块被boy吃掉。

       现在carlsson想要尽可能吃到更多的巧克力,但是如果当上述切较大的巧克力的步骤连续发生在同一块巧克力上会引起boy的怀疑。

       问carlsson最多能吃到多少巧克力。


分析:

       这道题目好难懂,读了好长时间。。。

       一开始毫无头绪,于是画图来看。模拟一下样例5*6,看做是5个1*6。显示分成了2*6和3*6,然后在3*6上动刀,将其变成1*6和2*6,把2*6吃掉剩下1*6。再然后在2*6上动刀,变成两个1*6,吃掉一个还剩一个,最后carlsson吃掉了4个1*6。

        我们看一下每次被动刀的巧克力的大小:

                                            5*6 → 3*6 → 2*6 → 1*6

        再看一下分8*6的巧克力的情况:

                                 8*6 → 5*6 → 3*6 → 2*6 → 1*6

        嘿嘿,有点意思了~

                                 8、5、3、2、1

        这是斐波那契!!!

        分析一下可以知道,用这种分法,只要知道了第一刀的切法,就知道最后的结果,但是第一刀太多,枚举不过来。那么,反过来想,从最后的一块入手,假设最后一块是a*b,那么它的前一块就一定是2a*b,在前面就是3a*b、5a*b、8a*b、13a*b……这样就发现是斐波那契了。

        需要能够吃到最多的巧克力,就得找出符合条件的最大的斐波那契数p,这个数是要能够被某一条边整除的。

        那怎么算出答案呢?

        找到这个斐波那契数之后就能知道a了,而这个a就是最后剩下来的巧克力的最小尺寸,那么carlsson就能吃到n*m-a*b这么大的巧克力,结果就是n*m - n*m / p。


代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;

LL f[46];

void fun(){
    f[1]=1;
    f[2]=2;
    for(int i=3;i<46;i++){
        f[i]=f[i-1]+f[i-2];
        //printf("%d\n",f[i]);
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    fun();
    int n,m;
    LL p;
    while(~scanf("%d%d",&n,&m)){
        int maxn=max(n,m);
        int flag=0;
        int i;
        for(i=2;f[i]<=maxn;i++){
            if(n%f[i]==0 || m%f[i]==0){
                p=f[i];
                flag=1;
                //cout<<p<<endl;
            }
        }
        if(!flag) printf("0\n");
        else{
            printf("%lld\n",(LL)n*(LL)m-(LL)n*(LL)m/p);
        }
        cout<<endl;
    }
    return 0;
}


基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值