BestCoder Round #75 King's Cake 模拟&&优化 || gcd
King's Cake
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.
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).
For each test case, print a single number as the answer.
2
2 3
2 5
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's Cake 模拟&&优化 || gcd的更多相关文章
- BestCoder Round #75 King's Order dp:数位dp
King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- BestCoder Round #75 1001 - King's Cake
Problem Description It is the king's birthday before the military parade . The ministers prepared a ...
- hdu 5643 BestCoder Round #75
King's Game Accepts: 249 Submissions: 671 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- hdu 5641 BestCoder Round #75
King's Phone Accepts: 310 Submissions: 2980 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- ACM学习历程—BestCoder Round #75
1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- 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 ...
- BestCoder Round #75 1002 - King's Phone
问题描述 阅兵式上,国王见到了很多新奇东西,包括一台安卓手机.他很快对手机的图形解锁产生了兴趣. 解锁界面是一个 3×33 \times 33×3 的正方形点阵,第一行的三个点标号 1,2,31, 2 ...
- BestCoder Round #75 1003 - King's Order
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...
随机推荐
- Scrum Meeting Alpha - 10
Scrum Meeting Alpha - 10 NewTeam 2017/11/06 地点:主楼和3号楼之间的走廊2楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了对涉及内容修改的API的 ...
- EM 算法求解高斯混合模型python实现
注:本文是对<统计学习方法>EM算法的一个简单总结. 1. 什么是EM算法? 引用书上的话: 概率模型有时既含有观测变量,又含有隐变量或者潜在变量.如果概率模型的变量都是观测变量,可以直接 ...
- C#中静态和非静态的区别
今天下午面试,HR问道:C#中静态类或静态方法和非静态类静态方法有什么区别?我回答是静态的可以直接调用而非静态的需要实例化.HR说这谁都知道,我问的是本质区别.我当时就郁闷了,我只有8个月的编程经验, ...
- PHP通过Zabbix API获取服务器监控信息
开源监控系统Zabbix提供了丰富的API,供第三方系统调用. 基本步骤如下: 1.获取合法认证:连接对应Zabbix URL,并提供用户名和密码,HTTP方法为"POST",HT ...
- firefox被hao123绑架的解决办法
1.在地址栏里输入"about:support" 2.单击配置文件夹后的"打开文件夹"按钮. 3.在弹出来的文件夹中找到那个叫做"user.js&qu ...
- Linux命令kill和signal
Linux命令kill和signal kill命令用于终止指定的进程(terminate a process),是Unix/Linux下进程管理的常用命令.通常,我们在需要终止某个或某些进程时,先使用 ...
- Python字符编码详解(转)
http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 在没有reload(sys)之前调用sys.setdefaultencoding ...
- MVC中提交包含HTML代码的页面处理方法(尤其是在使用kindeditor富文本编辑器的时候)
针对文本框中有HTML代码提交时,mvc的action默认会阻止提交,主要是出于安全考虑.如果有时候需求是要将HTML代码同表单一起提交,那么这时候我们可以采取以下两种办法实现: 1.给Control ...
- SQL 之存储过程
存储过程 是用来执行管理任务或应用复杂的业务规则, 存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数.输出参数.返回单个或多个结果集以及返回值. 存储过程的优点 存储过程已在服务器注册 执 ...
- openvpn部署之快速入门实战+一键部署openvpn脚本
个人原创禁止转载 软件环境: Centos6.9 x64 openvpn-2.4.3-1.el6.x86_64.rpm easy-rsa-2.2.2-1.el6.noarch.rpm #推荐使用 ...