zoukankan      html  css  js  c++  java
  • 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 }
    View Code
  • 相关阅读:
    Win7下使用TortoiseGit设置保存密码
    MacOS软件清单
    ubuntu安装python
    Mac使用SSH连接远程服务器
    CentOS常用命令
    Docker追加容器端口映射
    Docker安装CentOS7
    Windows操作路由表
    Docker部署MySQL8并实现远程连接
    Qt——容器类(译)
  • 原文地址:https://www.cnblogs.com/PrayG/p/5658422.html
Copyright © 2011-2022 走看看