codeforces 343A Rational Resistance

来源:互联网 发布:java程序开发培训价格 编辑:程序博客网 时间:2024/06/05 20:22

A. Rational Resistance


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

However, allMike has is lots of identical resistors with unit resistance R0 = 1. Elements withother resistance can be constructed from these resistors. In this problem, wewill consider the following as elements:

1.   one resistor;

2.   an element and one resistor plugged insequence;

3.   an element and one resistor plugged inparallel.

With theconsecutive connection the resistance of the new element equals R = Re + R0. With theparallel connection the resistance of the new element equals . In this case Re equals theresistance of the element being connected.

Mike needs toassemble an element with a resistance equal to the fraction . Determine the smallest possible numberof resistors he needs to make such an element.

Input

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

Output

Print a singlenumber — the answer to the problem.

Please do notuse the %lld specifierto read or write 64-bit integers in С++. It is recommended to use the cincout streams orthe%I64d specifier.

Sample test(s)

input

1 1

output

1

input

3 2

output

3

input

199 200

output

200

Note

In the firstsample, one resistor is enough.

In the secondsample one can connect the resistors in parallel, take the resulting elementand connect it to a third resistor consecutively. Then, we get an element withresistance . We cannot make this element using tworesistors.

 

 

题意:

         给出 两个数A,B;要求用最少的阻值为1的电阻串联、并联在一起组成阻值为A/B的电阻;输出最少的电阻数量;

 

思路:

         对于电阻来说将k个电阻串联和并联关系完全反转的话,阻值的变化特性是变为原阻值的倒数,因而我们可以利用这一特性不停的将并联关系转化为串联关系,而A/B可化为假分数形式n+a/B,期中整数部分由n个电阻串联,分数部分由一个并联电阻组组成,将并联化为串联关系,即将分数部分倒过来,不断反复,累加得到答案;

 

代码

 

#include <bits/stdc++.h>using namespace std;typedef long long ll;int main(){   ll a,b;   cin >>a>>b;   ll tmp, ans = 0;   while(b){       ans += a/b;       a %= b;       swap(a,b);    }   cout << ans;}

0 0
原创粉丝点击