King's Cake

Accepts: 967
Submissions: 1572
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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 test cases.

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

Output

For each test case, print a single number as the answer.

Sample Input
Copy

2
2 3
2 5
Sample Output
Copy

3
4 hint:
For the first testcase you can divide the into one cake of 2×2 , 2 cakes of 1×1

source

The question is from BC
and hduoj 5640.

My Solution

//A really easy problem, I get a Runtime Error(STACK_OVERFLOW) first, then Time Limit Exceeded

//next Runtime Error (INTEGER_DIVIDE_BY_ZERO), and a WA   , Accepted......

//I am really sorry for that.

1、用循环模拟

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int tot; int main()
{
int T, l, s, t;
scanf("%d", &T);
while(T--){
scanf("%d%d", &l, &s);
if(l < s) swap(l, s);
tot = 0;
while(true){
if(s == 1) {tot += l; break;}
if( s == 0) break; //!
t = l/s;
l -= t*s;
if(l < s) swap(l, s);
tot += t;
} printf("%d\n", tot);
}
return 0;
}

2、像是求最大公约数。所以每次 gcd 的时候累加答案就可以,复杂度O(logmax(n,m)T)。

Thank you!

BestCoder Round #75 King&#39;s Cake 模拟&amp;&amp;优化 || gcd的更多相关文章

  1. BestCoder Round #75 King&#39;s Order dp:数位dp

    King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...

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

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

  3. hdu 5643 BestCoder Round #75

    King's Game  Accepts: 249  Submissions: 671  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  4. hdu 5641 BestCoder Round #75

    King's Phone  Accepts: 310  Submissions: 2980  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  5. ACM学习历程—BestCoder Round #75

    1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...

  6. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

  7. hdu 5640 King's Cake(BestCoder Round #75)

    King's Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. BestCoder Round #75 1002 - King's Phone

    问题描述 阅兵式上,国王见到了很多新奇东西,包括一台安卓手机.他很快对手机的图形解锁产生了兴趣. 解锁界面是一个 3×33 \times 33×3 的正方形点阵,第一行的三个点标号 1,2,31, 2 ...

  9. BestCoder Round #75 1003 - King's Order

    国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...

随机推荐

  1. PTA 数据结构 银行业务队列简单模拟

    仅供参考,请勿粘贴 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达银行的顾客序列,请按业务完 ...

  2. ARM开发板链接shell

    1.用网线插入开发板(最好链接路由器) 2.启动开发板(可以用U盘启动) 执行 #run bootusb 3.联网 #ifconfig eth0 up #udhcpc或者#dhclient wan # ...

  3. Socket网络编程之概述理解

    今天主要讲讲什么是socket网络编程 socketde 英文原义是"孔"或者"插座".是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机 ...

  4. java面试扫盲:对象初始化顺序(真的未必能答对)

    记录下面试里面遇到的一些java盲区,一方面扫描自己的知识盲区,一方面也可以给后面面试的朋友一些警示,以免面试的时候出现不知道的尴尬情况. 提出问题:父类静态属性,父类属性,父类构造方法,子类静态属性 ...

  5. [转载] HDFS简介

    转载自http://www.csdn.net/article/2010-11-26/282582 http://subject.csdn.net/hadoop/ 一.HDFS的基本概念 1.1.数据块 ...

  6. Node学习笔记 http

    node url querystring 第二个参数指定分隔符 也可以指定三个参数,效果和两个参数类似 不同于querystring,下面是querystringfy的用法 queryescape与e ...

  7. FPGA DDR3调试

    FPGA DDR3调试 Spartan6 FPGA芯片中集成了MCB硬核,它可以支持到DDR3.在Xilinx的开发工具Xilinx ISE中提供了MIG IP核,设计者可以用它来直接生成 DDR3 ...

  8. PHP截取带有汉字的字符串,将汉字按两个字节计算

    <?php header("Content-type:text/html;charset=utf-8"); /** *截取字符串,汉字占两个字节,字母占一个字节 *页面编码必 ...

  9. 想使用Docker容器?先看看这些注意事项

    Docker容器无疑是最近十年来最引人注目的技术之一,因为有了它,对我们思考设计.开发和运维软件的方式产生了非常有益的影响. 但是就像每一个开发工具一样,为了充分利用这些工具,需要注意一些使用中问题, ...

  10. 图片转换base64数据上传,并且实现预览的简便方法

    对于很多新手来说,实现上传图片并且预览功能,都会感到不知所可,然后开始在网站搜索各种各样的图片上传预览插件,但是有的时候我们只是想简单的实现判断格式,以及预览的功能,使用插件的话,会使得项目的资源空间 ...