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
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...
随机推荐
- JAVA 后台SSM框架接收安卓端的json数据
最近项目上与安卓端做JSON数据交互,使用的SSM框架,刚开始的时候感觉很简单,想着不就是把安卓端的JSON数据封装为Bean类对象吗? 于是就这样写了 可是这样一直报400,百度原因是因为请求url ...
- Mybatis主配置文件常见使用讲解
在开发中我们通常将Mybatis中配置文件分两种,主配置文件与和dao对应的映射文件. 其实最后mybatis解析的还是一个主配置文件. 而映射文件会通过我们配置<mappers>属性,或 ...
- C# 链接MySql数据库
C# 链接MySql数据库只得注意的几点: 1.C#链接MySql数据库要在网上下载一个mysql-connector-net-6.0.4-noinstall.rar 这里面放的都是一堆dll .将 ...
- 无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
预览效果图: Selenium 数据库结构: id(int) classname(string) parentid(int) sort(int用于显示与排序) 1 家居 0 1 2 家电 0 ...
- TensorFlow(三)---------正则化
TensorFlow正则化经常被用于Deep-Learn中,泛化数据模型,解决过拟合问题.再深度学习网络只有在有足够大的数据集时才能产生惊人的学习效果.当数据量不够时,过拟合的问题就会经常发生.然而, ...
- Python mysqldb模块
#!/usr/bin/env python2.7 #-*- coding:utf8 -*- import os import sys import logging import MySQLdb fro ...
- Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...
- python 开源全文检索工具 Whoosh
About Whoosh Whoosh is a fast, featureful full-text indexing and searching library implemented in pu ...
- Python For嵌套循环 图形打印X型 nested loop -练习题
For嵌套循环图形打印作业很多是C++语言做的,我觉得Python应该也能做,就来试一试. 原网址C++练习题:http://www.imooc.com/qadetail/216848?t=33880 ...
- mysql 5.7.13 安装配置方法图文教程(linux) (转)
http://www.jb51.net/article/87160.htm ************************************************ linux环境Mysql ...