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. CopyOnWriteArrayList并发容器

    CopyOnWriteArrayList并发容器 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才 ...

  2. JS中的循环---最全的循环总结

    在讲循环的之前,先知道一下循环结构的执行步骤 1.声明循环变量: 2.判断循环条件; 3.执行循环体操作: 4.更新循环变量: 5.然后循环执行2-4,直到条件不成立,跳出循环. 1while循环 v ...

  3. C#保留小数位数的方法

    1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();provi ...

  4. 雅虎公司C#笔试题及参考答案

    Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...

  5. 使用Dropwizard(2)-配置分类ConfiguredBundle

    前言 不可避免的要用dropwizard作为service框架.持续学习.上次在dropwizard中使用feign,使用hystrix, 算是基本入门了.接下来就是基于此的优化. 把需要使用Conf ...

  6. iOS 远程推送消息解析及逻辑处理

    关于远程推送的相关配置网上已经有足够多的教程,这里就不复述了.这里讲述当客户端收到推送消息后,应怎样对其进行相应的逻辑处理. 工程的AppDelegate.m文件里提供了如下方法: //当应用程序启动 ...

  7. Win32API起始处的mov edi, edi与用户空间InlineHook

    在x86平台上,无论是在调试器中跟到系统DLL中时,还是反汇编某个系统DLL时,经常会发现很多API的第一条汇编指令都是mov edi, edi.根据经验来讲,C函数的汇编形式,应该是首先push e ...

  8. 前端worker之web worker

    web worker 背景 众所周知javascript是单线程的,同一时间内只能做一件事情. 这是十分必要的,设想,如果js是多线程的.有个dom元素两个线程同时做了改变,一个display:non ...

  9. sklearn的简单使用

    import numpy as np from sklearn import datasets from sklearn.cross_validation import train_test_spli ...

  10. selenium与表格的二三事

    今天遇到的问题是selenium与表格中行和列的问题! 我想要做的事情是统计当前的table有多少行,表格形式如下如所示: 图中所示为2行,我的定位方式是这样的 : table=driver.find ...