Codeforces Round #200 (Div. 1)A. Rational Resistance 数学
A. Rational Resistance
Time Limit: 1 Sec ?
Memory Limit: 256 MB
題目連接
http://codeforces.com/contest/343/problem/ADescription
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:
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
199 200Sample Output
200?
HINT
?
題意
你有無數(shù)個1歐的電阻,要求你用并聯(lián)和串聯(lián)構(gòu)成a/b歐的電阻
問你最少需要多少個電阻
題解:
首先結(jié)構(gòu)肯定是串聯(lián)加并聯(lián)啦
a/b,整數(shù)部分由串聯(lián)構(gòu)成,分?jǐn)?shù)部分由并聯(lián)構(gòu)成就好了
這樣就可以不停的遞歸了其實
代碼:
#include<stdio.h> #include<math.h> #include<iostream> using namespace std;int main() {long long a,b;scanf("%lld%lld",&a,&b);if(a<b)swap(a,b);long long ans = 0;while(a&&b){ans += a/b;a%=b;swap(a,b);}printf("%lld\n",ans); }?
轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/4870943.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #200 (Div. 1)A. Rational Resistance 数学的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HOJ-1007
- 下一篇: 大道至简第三章读后感