King's Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 736    Accepted Submission(s): 539

Problem Description
It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut the rectangle cake into two pieces, one of which should be a square cake.. Since he loves squares , he will cut the biggest square cake. He will continue to do that until all the pieces are square. Now can you tell him how many pieces he can get when he finishes.
 
Input
The first line contains a number T(T≤1000), the number of the testcases.

For each testcase, the first line and the only line contains two positive numbers n,m(1≤n,m≤10000).

 
Output
For each testcase, print a single number as the answer.
 
Sample Input
2
2 3
2 5
 
Sample Output
3
4
hint:
For the first testcase you can divide the into one cake of $2\times2$ , 2 cakes of $1\times 1$
 
题意:n*m的蛋糕,每次切一刀,要求有一块是正方形,问当最后所有蛋糕都是正方形时有多少块
题解:1、当长宽相等时 2、当长宽不等时要求长或宽有一个等1
#include<stdio.h>
#include<string.h>
#include<cstdio>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD double
#define MAX 3000010
#define mod 100
#define dian 1.000000011
#define INF 0x3f3f3f
using namespace std;
int main()
{
LL n,m,j,i,k,sum,Max,Min;
int t;
scanf("%d",&t);
while(t--)
{
//printf("1\n");
scanf("%lld%lld",&n,&m);
if(n==m)
{
printf("1\n");
continue;
}
int x;
sum=0;k=0;
Min=min(n,m);
Max=max(n,m);
while(Max!=1&&Min!=1)
{
Max-=Min;
k++;
if(Max<Min)
{
x=Max;
Max=Min;
Min=x;
}
if(Max==Min)
break;
}
if(Max==Min)
printf("%lld\n",k+1);
else if(Max!=Min&&Min==1)
{
sum=sum+k+Max;
printf("%lld\n",sum);
}
}
return 0;
}

  

 

hdu 5640 King's Cake(BestCoder Round #75)的更多相关文章

  1. hdu 5640 King's Cake(模拟)

    Problem Description   It is the king's birthday before the military parade . The ministers prepared ...

  2. HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

    若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...

  3. HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

    先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...

  4. hdu 5641 King's Phone(暴力模拟题)

    Problem Description In a military parade, the King sees lots of new things, including an Andriod Pho ...

  5. HDU 5640 King's Cake GCD

    King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...

  6. HDU 5640 King's Cake

    King's Cake Problem Description It is the king's birthday before the military parade . The ministers ...

  7. hdu 5063 Operation the Sequence(Bestcoder Round #13)

    Operation the Sequence                                                                     Time Limi ...

  8. HDU 5640 King's Cake【模拟】

    题意: 给定长方形,每次从中切去一个最大的正方形,问最终可以得到多少正方形. 分析: 过程类似求gcd,每次减去最小的边即可. 代码: #include <cstdio> #include ...

  9. BestCoder Round #75 1001 - King's Cake

    Problem Description It is the king's birthday before the military parade . The ministers prepared a ...

随机推荐

  1. 函数buf_page_hash_get_low

    /******************************************************************//** Returns the control block of ...

  2. 基于XMPP的即时通信系统的建立(二)— XMPP详解

    XMPP详解 XMPP(eXtensible Messaging and Presence Protocol,可扩展消息处理和现场协议)是一种在两个地点间传递小型结构化数据的协议.在此基础上,XMPP ...

  3. CodeForces 489A (瞎搞) SwapSort

    题意: 给n个整数(可能有重复),输出一个不超过n次交换的方案,使得经过这n次交换后,整个序列正好是非递减的. 分析: 首先说题解给的算法. 从左到右扫一遍,交换第i个数和它后面最小的那个数. 代码看 ...

  4. UVa 10129 Play On Words【欧拉道路 并查集 】

    题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...

  5. ASP.NET在IE10,IE11中Form表单身份验证失效问题解决方法

    已经研究出解决方案. 在web.config中的forms中增加cookieless="UseCookies"属性即可.   <authentication mode=&qu ...

  6. Relativelayout属性

    // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below 将该控件的底部置于给定ID的控件之下; andro ...

  7. [转]Jquery Ajax用法

    原文地址:http://www.php100.com/html/program/jquery/2013/0905/6004.html jQuery学习之jQuery Ajax用法详解 来源:   时间 ...

  8. Deferred的那些知识

    在移动开发中的各种中,我们一定会遇到异步回调的问题,比如: 1:Css3执行动画完毕, 回调 2:Jquery Animate动画的执行完毕, 回调 3:Ajax的执行(并行.串行),回调 等等   ...

  9. hdu 2204 Eddy's爱好

    // 一个整数N,1<=N<=1000000000000000000(10^18).// 输出在在1到N之间形式如M^K的数的总数// 容斥原理// 枚举k=集合{2,3,5,7,11,1 ...

  10. Android UncaughtExceptionHandler,捕获错误

    最近在做个项目,需要在程序出现运行时异常和错误导致程序crash时进行一些操作,找到一个方法 Thread.setDefaultUncaughtExceptionHandler(new Uncaugh ...