Description

对于一个数字对(a, b),我们可以通过一次操作将其变为新数字对(a+b, b)或(a, a+b)。

给定一正整数n,问最少需要多少次操作可将数字对(1, 1)变为一个数字对,该数字对至少有一个数字为n。

Input

第一行一个正整数 n

Output

一个整数表示答案。

Sample Input

5

Sample Output

3

Hint

样例解释:

(1,1)  →  (1,2)  →  (3,2)  →  (5,2)

对于30%的数据, 1 <= n <= 1000

对于60%的数据, 1 <= n <= 20000

对于100%的数据,1 <= n <= 10^6

题解

我们注意到对于数对$(x,y)$,$x>y$,一定是由$(x-y,y)$推得的,我们发现就是更相减损术。

我们可以枚举$<n$的所有数,做一遍$gcd$,统计操作次数。

最终,如果其中一个数为$1$,那么我们可以去更新一下答案;

但如果为$0$,即最初始数对不合法,舍去。

 #include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,cnt;
int gcd(int a,int b)
{
int cnt=;
while (true)
{
if (b==) return ;
if (b==)
{
cnt+=a-;
break;
}
cnt+=a/b;
a%=b;
swap(a,b);
}
return cnt;
} int main()
{
scanf("%d",&n);
int ans=~0u>>;
if (n==) printf("0\n");
else
for (int i=;i<n;i++)
{
int cnt=gcd(n,i);
if (cnt) ans=min(ans,cnt);
}
printf("%d\n",ans);
return ;
}

最新文章

  1. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
  2. selenium webdriver学习(一)
  3. PHP学习之常量
  4. PowerShell 启动应用程序【转】
  5. redmine问题
  6. IOS crash分析
  7. [转贴]使用CryptoAPI解析X509证书和P12证书
  8. Java多线程同步方法Synchronized和volatile
  9. 郝斌老师C语言学习笔记(一)
  10. Webpack 2 视频教程 009 - 配置 ESLint 实现代码规范自动测试 (上)
  11. asp.net core轻松入门之MVC中Options读取配置文件
  12. 【学习笔记】node.js重构路由功能
  13. Solidity高级理论(二):Gas
  14. [转载]sql 盲注之正则表达式攻击
  15. 4. Father&#39;s Impact on a Child&#39;s Language Development 父亲对孩子语言发展的影响
  16. 解决Windows x86网易云音乐不能将音乐下载到SD卡的BUG
  17. ASP入门(十七)-ASP #include
  18. IIS 之 连接数、并发连接数、最大并发工作线程数、队列长度、最大工作进程数
  19. A - 最少拦截系统 (最长上升子序列)
  20. Codeforces 543.B Destroying Roads

热门文章

  1. QT5.8 for embedded
  2. Beta阶段敏捷冲刺报告-DAY2
  3. 用phpcms切换中英文网页的方法(不用解析二级域名)、phpcms完成pc和手机端切换(同一域名)
  4. sublimeText3 中配置sass环境,并将编译后文件保存到指定文件夹
  5. Spring知识点回顾(06)Profile 和 条件注解 @Conditional
  6. Docker的容器操作
  7. Spring Security入门(3-5)Spring Security 的鉴权 - 决策管理器和投票器
  8. matlab 对tif数据高程图的处理分析
  9. leetcode算法:Island Perimeter
  10. oracle获取表字段属性