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 cin, cout 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;}
- CodeForces 343A Rational Resistance
- CodeForces 343A Rational Resistance
- codeforces 343A Rational Resistance
- codeforces343A A. Rational Resistance
- A. Rational Resistance----贪心
- Codeforces 344C - Rational Resistance
- codeforces #200 div2.C Rational Resistance [思维]
- Codeforces 344C Rational Resistance【思维+贪心】
- Rational Resistance
- Codeforces Round #200 (Div. 2) C. Rational Resistance
- CodeForces - 344C Rational Resistance (模拟题 +递归求解)
- Codeforces Round #200 (Div. 2)344C Rational Resistance(模拟)
- Codeforces 501C Rational Resistance 迭代+思维
- C. Rational Resistance
- Codeforces Round #200 (Div. 2) C. Rational Resistance(脑洞思维)
- Resistance
- CF#200 div2 C Rational Resistance(math gcd)
- CodeForces 343A
- Eclipse开发必备干货分享
- Snowflake Snow Snowflakes
- sqlserver 时间处理函数
- 堆排序
- Android布局笔记
- codeforces 343A Rational Resistance
- 关于解压文档名中文出现乱码解决方案
- iOS -- git的常用命令,使用步骤
- 一道SQL面试例题 if...else 与聚集函数
- [LeetCode 190]Reverse Bits
- C++编译器无法捕捉到的8种错误
- linux命令中的head命令
- CSS3手机浏览器兼容情况
- PHP初学-io操作