Description

Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.

However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:

  1. one resistor;
  2. an element and one resistor plugged in sequence;
  3. an element and one resistor plugged in parallel.

With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals . In this case Re equals the resistance of the element being connected.

Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element.

Input

The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 1018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists.

Output

Print a single number — the answer to the problem.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.

Sample Input

Input
1 1
Output
1
Input
3 2
Output
3
Input
199 200
Output
200

Hint

In the first sample, one resistor is enough.

In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance . We cannot make this element using two resistors.

题意: 给你2个数a,b,现在有无穷的电阻为一的电阻可以拿,想要组成一个阻值为a/b的电阻,求最少需要多少个阻值为1 的电阻?

分析: 写了挺久,这tm是个简单数学题啊啊啊啊!其实就是按照最大公因数的方式一步步求解,对于给定的a,b,如果a < b,我们交换a,b,的值,并用a/b得到这次对答案的贡献,然后更新a的值,循环处理直到分母为0 ,得到答案。。。。。

 /*************************************************************************
> File Name: cf.cpp
> Author:
> Mail:
> Created Time: 2016年07月10日 星期日 17时04分04秒
************************************************************************/ #include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(ll a,ll b,ll &ans)
{
if(a < b)
{
swap(a,b);
}
while(b)
{
ans += a/b;
a = a%b;
swap(a,b);
}
return ans;
}
int main()
{
ll a,b;
cin >> a >> b;
ll ans= ;
ans = solve(a,b,ans);
cout << ans << endl;
return ;
}

最新文章

  1. java向Excel文件写入数据
  2. android 取消edittext焦点
  3. Pycharm: keyboard reference
  4. C#对图片文件的压缩、裁剪操作
  5. bootstrap弹出框提示框无法调用
  6. pullToRefresh下拉刷新上拉加载
  7. pupper基线加固
  8. HTML5 增强的页面元素
  9. Bzoj 4196: [Noi2015]软件包管理器 树链剖分
  10. C# 中的关键字之:base、this 【转】
  11. Android应用开发学习笔记之播放视频
  12. 【BZOJ 3926】【ZJOI 2015】诸神眷顾的幻想乡
  13. IOC杂谈(一)初识IOC
  14. FastReport.Net
  15. 运行svn tortoiseSvn cleanup 命令失败的解决办法
  16. 思考与算法:大脑是cpu、思考是算法
  17. elastic-job详解(一):数据分片
  18. 搭建 PhoneGap 开发环境
  19. 遍历目录大小——php经典实例
  20. Mybatis源码分析之Mapper文件解析

热门文章

  1. CSS 子元素选择器
  2. C语言修改文件某部分内容
  3. 破解者是如何篡改游戏内数值的,揭秘Android手游破解全过程
  4. _stat函数/struct stat 结构体使用笔记
  5. HDU 2196 Computer(求树上每一个节点到其他点的最远距离)
  6. Ubuntu下用glade和GTK+开发C语言界面程序(一)
  7. IOS开发的哪些异常之异常断点
  8. 2、Python列表和元组
  9. 代理server poll version
  10. 迷宫求解_数据结构c语言版