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. 【Kafka源码】ReplicaManager启动过程

    在KafkaServer启动过程的入口中,会启动Replica Manager,众所周知,这是一个副本管理器.replica在Kafka中扮演的角色很重要,是保证消息不丢失的一个重要概念. repli ...

  2. 532. K-diff Pairs in an Array

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  3. Linux修改date

    与cn校时 ntpdate cn.pool.ntp.org 设置时区 TZ='Asia/Shanghai'; export TZ 刷新生效 source ~/.bashrc vi /etc/profi ...

  4. Git(3)----Eclipse上Git插件使用技巧

    转载:http://blog.csdn.net/qq_33066205/article/details/56675704 一_安装EGIT插件 http://download.eclipse.org/ ...

  5. C#编程命名规范推荐

    1.用Pascal规则来命名方法和类型. Pascal 大小写形式是指名称中的单词的第一个字母大写public class DataGrid{public void DataBind(){ }} 2. ...

  6. Transact-SQL知识点梳理

    Transact-SQL基础语言 运行环境:SQL Server 语法约定: 语法约定 用途说明 大写字母 Transact-SQL关键字 斜体 用户提供的Transact-SQL语法参数 粗体 数据 ...

  7. 使用sshkey的方式访问gitlab

    在使用jenkins创建jobs的时候配置git为ssh访问的方式报错 记录错误信息 Failed to connect to repository : Command "/usr/bin/ ...

  8. 学习java的优势

    学习java之后,到企业的岗位 技术:java软件开发工程师(中初级):技术一般: 高级工程师:技术高等: 技术架构师:技术顶级: 管理:项目经理:产品经理: 质询:质询顾问:销售经理: 学会之后可以 ...

  9. 去除url后缀(.html,.jsp等)的有效方法

    有时候,我们想要隐藏真正的路径,或者觉着很酷,举一个知乎网的例子,如https://www.zhihu.com/question/39547745 那么?如何做到呢,其实,可以有一个非常简单而有效的解 ...

  10. android studio 默认 .gitignore 文件模板

    # built application files*.apk*.ap_ # files for the dex VM*.dex # Java class files*.class # generate ...