Codeforces 344C Rational Resistance

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 }

时间: 2024-12-22 14:26:43

Codeforces 344C Rational Resistance的相关文章

[CodeForces 344C Rational Resistance]YY,证明

题意:给若干个阻值为1的电阻,要得到阻值为a/b的电阻最少需要多少个. 思路:令a=mb+n,则a/b=m+n/b=m+1/(b/n),令f(a,b)表示得到a/b的电阻的答案,由f(a,b)=f(b,a),有: f(a,b)=a/b + f(a%b,b)=a/b+f(b,a%b) (1)由于将所有的电阻之间的关系改变一下,串联变成并联,并联变成串联,阻值变成之前的倒数,所以f(a,b)=f(b,a)成立. (2)现在再证一下:串联变成并联,并联变成串联,阻值变成之前的倒数.考虑任一个电路,一定

Codeforces 343A. Rational Resistance

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

CodeForces 344

这次是作为晚训题目做的,代码都挺短,考的是思维,想到了分分钟AC,想不到就依然是想不到-- A - Magnets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 344A Description Mad scientist Mike entertains himself by arranging rows of domi

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

zzu--2014年11月16日月赛 B题

1229: Rational Resistance Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 8  Solved: 4 [Submit][Status][Web Board] Description Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain res

zzu--2014年11月16日月潭赛 B称号

1229: Rational Resistance Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 8  Solved: 4 [id=1229" style="color:rgb(26,92,200); text-decoration:none">Submit][Status][Web Board] Description Mad scientist Mike is building a time machine in h

uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)

题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,任何一点的电流向量为0.就是说有多少电流流入该节点,就有多少电流流出. 对于每次询问的两点间等效电阻,先判断说两点是否联通,不连通的话绝逼是1/0(无穷大).联通的话,将同一个联通分量上的节点都扣出来,假设电势作为变元,然后根据基尔霍夫定律列出方程,因为对于每个节点的电流向量为0,所以每个节点都有一个方程,所有与该节点

Codeforces Round #412 C. Success Rate

Success Rate You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x?/?y. Your f

UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)

UVA 10808 - Rational Resistors 题意:给定一些结点,有一些电阻,电阻分布在边上,给定一个电路图,每次询问两点,求这两点间的等效电阻 思路:根据基尔霍夫定律,任意一点的电流向量为0,这样就能设每个结点的电势,列出方程,利用高斯消元求解,对于无解的情况,肯定是两点不能连通,这个可以利用并查集判断. 此外这题有个很坑的地方啊,就是高斯消元的姿势不够优美就会爆long long 代码: #include <cstdio> #include <cstring>