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
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...
随机推荐
- Netty(二)——TCP粘包/拆包
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7814644.html 前面讲到:Netty(一)--Netty入门程序 主要内容: TCP粘包/拆包的基础知 ...
- 【OpenCV】通过ROI区域以及掩码实现图像叠加
在图像处理领域,我们常常需要设置感兴趣区域(ROI,region of interest),来专注或者简化我们的工作过程 .也就是从图像中选择的一个图像区域,这个区域是我们图像分析所关注的重点.我们圈 ...
- 【转】MYSQL DBA知识了解-面试准备
http://www.itpub.net/forum.php?mod=viewthread&tid=1825849 公司招聘MySQL DBA,也面试了10个2年MySQL DBA工作经验的朋 ...
- ExtJS+Handler入门显示
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...
- 1)C语言简介(C自考学习)
C语言历史由来 世界上第一个高级语言是"ALFOL",而C的前身是ALGOL语言.1970年美国贝尔实验室的肯·汤普逊对BCPL(基本复合程序设计语言)进行了进一步的简化,突出了硬 ...
- 对java泛型的理解
正确的应用java泛型的特性可以更好的实现编程的开闭原则(对扩展开放,对修改关闭),这得益于java泛型提供的在程序运行时获取对象声明类型的特性. 静态语言的特性是在程序编译前进行声明,这样程序在编译 ...
- 做技术,有没有必要参加IT培训
近几年,IT培训机构可谓是琳琅满目,稂莠不齐.培训Java的,培训PHP的,培训大数据的等等吧,不一而足. 自己也算是IT技术圈子待了好多年了,面试过一些机构培训出来的学生,也有几个好哥们在培训机构做 ...
- python学习笔记 改变字符串中的某一位
a = ' a = list(a) a[2] = ' news = ''.join(a) print news,a 注意不能使用 news = '' news.join(a) 因为news.join只 ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- 计算出前N项的数据
#include<iostream> #include<algorithm> #include<numeric> using namespace std; ; in ...