# 42. Pairs of Numbers

https://blog.csdn.net/qq_43521140/article/details/107853492

- 出题人:OJ
- 标签:["DFS and Similar"]
- 难度:简单
- 总分数:100

## 题目描述
<p>Let's assume that we have a pair of numbers (a,b). We can get a new pair (a+b,b) or (a,a+b) from the given pair in a single step.</p><p>Let the initial pair of numbers be (1,1). Your task is to find number k, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n.</p>

## 解答要求
时间限制:1000ms, 内存限制:100MB

## 输入
<p>The input contains the only integer n (1 ≤ n ≤ 10<sup>6</sup>).<b>Process to the end of file</b>.</p>

## 输出
<p>Print the only integer k.</p>

## 样例
### 输入样例 1:
```
5
1
```
### 输出样例 1:
```
3
0

```
## 提示

```
package main
import "fmt"
var tmp int
var leastep int
func main() {
var n int
for {
_, err := fmt.Scanf("%d", &n)
if err != nil {
return
} else if n == 1 {
fmt.Printf("0\n")
continue
} else if n == 2 {
fmt.Printf("1\n")
continue
}
leastep = n - 1
for i:= 1;i<n;i++{
tmp = 0
dfs(n,i)
leastep = minint(tmp,leastep)
}
//leastep = n + 1
//for i := n/2 + 1; i < n && i/(n-i) <= leastep; i++ {
// leastep = minint(leastep, leaststeps(i, n-i, 1))
//}
fmt.Printf("%d\n", leastep)
}
}
func minint(a, b int) int {
if a >= b {
return b
} else {
return a
}
}
func dfs(a,b int){
if b == 1{
tmp += a-1
return
}
tmp += a/b
dfs(b,a%b)
}

func leaststeps(a, b, steps int) int {
if b == 1 {
return a - 1 + steps
} else if a%b == 0 {
return leastep + 1
} else if steps >= leastep {
return leastep + 1
} else {
return leaststeps(b, a%b, steps+a/b)
}
}

```

Pairs of Numbers 辗转相除的更多相关文章

  1. Alice and Bob 要用到辗转相减

    Alice and BobTime Limit: 1 Sec  Memory Limit: 64 MBSubmit: 255  Solved: 43 Description Alice is a be ...

  2. bzoj 2852: 强大的区间 辗转相除

    2852: 强大的区间 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 12[Submit][Status][Discuss] D ...

  3. (中等) CF 585C Alice, Bob, Oranges and Apples,矩阵+辗转相除。

    Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples ...

  4. BZOJ.4031.[HEOI2015]小Z的房间(Matrix Tree定理 辗转相除)

    题目链接 辗转相除解行列式的具体实现? 行列式的基本性质. //864kb 64ms //裸的Matrix Tree定理.练习一下用辗转相除解行列式.(因为模数不是质数,所以不能直接乘逆元来高斯消元. ...

  5. [Luogu1891]疯狂LCM[辗转相减法]

    题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...

  6. Luogu4111 [HEOI2015]小Z的房间 (矩阵树,辗转相除高斯消元)

    除法不能用于同余系,要辗转相除.注意不能加入柱子到矩阵. #include <iostream> #include <cstdio> #include <cstring& ...

  7. CF 403D Beautiful Pairs of Numbers

    The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...

  8. 数据结构与算法C++描述学习笔记1、辗转相除——欧几里得算法

    前面学了一个星期的C++,以前阅读C++代码有些困难,现在好一些了.做了一些NOI的题目,这也是一个长期的目标中的一环.做到动态规划的相关题目时发现很多问题思考不通透,所以开始系统学习.学习的第一本是 ...

  9. POJ 2348 Euclid's Game(辗转相除博弈+自由度分析)

    题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其 ...

  10. Pairs of Numbers

    #include<stdio.h> //we have defined the necessary header files here for this problem. //If add ...

随机推荐

  1. 第四十六篇:工程化概念以及什么是webpack

    好家伙,这波是概念补充 1.什么是工程化概念? 我的开发: 开个项目,想怎么改怎么改,拉个东西过来就用 实际的前端开发: (1) 模块化(js的模块化,css的模块化,资源的模块化) (2) 组件化( ...

  2. IDEA整合Docker

    创建项目 1.使用 IDEA 构建一个 SpringBoot 项目 2.编写一个helloController @RestController public class HelloController ...

  3. 如何使用helm优雅安装prometheus-operator,并监控k8s集群微服务

    前言:随着云原生概念盛行,对于容器.服务.节点以及集群的监控变得越来越重要.Prometheus 作为 Kubernetes 监控的事实标准,有着强大的功能和良好的生态.但是它不支持分布式,不支持数据 ...

  4. java 类名后加变量名是什么意思?

    回答这个问题我们需要先了解两个事情: A是一个类,我们如果对他进行实例化,需要这样写: A a = new A(); 详细解释一下这个语句,首先等号左边做的事情:在JVM栈内存(stack)中定义了一 ...

  5. 使用 Docker 安装 Elastic Stack 8.0 并开始使用

    文章转载自:https://mp.weixin.qq.com/s/fLnIzbbqYfILS6uCvGctXw 运行 Elasticsearch docker network create elast ...

  6. Security Context

    概述 Security Context(安全上下文)用来限制容器对宿主节点的可访问范围,以避免容器非法操作宿主节点的系统级别的内容,使得节点的系统或者节点上其他容器组受到影响. Security Co ...

  7. docker还原挂载在宿主机的mysql数据

    ps:有个项目的数据库之前是docker的mysql镜像,数据都去mysql的镜像里面拿数据,但是目前镜像里面的数据库是空的,所以现在来说明一下如何恢复数据 1.创建数据卷 docker volume ...

  8. 微信小程序之发起请求

    wx.request({ url: api.api + '/weChat/api/user/myAunt', // 仅为示例,并非真实的接口地址 data: {}, method: 'GET', he ...

  9. NOI2015 洛谷P1955 程序自动分析(并查集+离散化)

    这可能是我目前做过的最简单的一道noi题目了...... 先对e=1的处理,用并查集:再对e=0查询,如果这两个在同一集合中,则为""NO",最后都满足的话输出" ...

  10. MySQL 窗口函数

    1. 窗口函数概念和语法 窗口函数对一组查询行执行类似聚合的操作.然而,聚合操作将查询行分组到单个结果行,而窗口函数为每个查询行产生一个结果: 函数求值发生的行称为当前行 与发生函数求值的当前行相关的 ...