码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 344C Rational Resistance

时间:2016-07-10 21:32:58      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

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 ,得到答案。。。。。

技术分享
 1 /*************************************************************************
 2     > File Name: cf.cpp
 3     > Author: 
 4     > Mail: 
 5     > Created Time: 2016年07月10日 星期日 17时04分04秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<bits/stdc++.h>
10 using namespace std;
11 typedef long long ll;
12 ll solve(ll a,ll b,ll &ans)
13 {
14     if(a < b)
15     {
16         swap(a,b);
17     }
18     while(b)
19     {
20         ans += a/b;
21         a = a%b;
22         swap(a,b);
23     }
24     return ans;
25 }
26 int main()
27 {
28     ll a,b;
29     cin >> a >> b;
30     ll ans= 0;
31     ans = solve(a,b,ans);
32     cout << ans << endl;
33     return 0;
34 }
View Code

 

Codeforces 344C Rational Resistance

标签:

原文地址:http://www.cnblogs.com/PrayG/p/5658422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!