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

 /*************************************************************************
> File Name: cf.cpp
> Author:
> Mail:
> Created Time: 2016年07月10日 星期日 17时04分04秒
************************************************************************/ #include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(ll a,ll b,ll &ans)
{
if(a < b)
{
swap(a,b);
}
while(b)
{
ans += a/b;
a = a%b;
swap(a,b);
}
return ans;
}
int main()
{
ll a,b;
cin >> a >> b;
ll ans= ;
ans = solve(a,b,ans);
cout << ans << endl;
return ;
}

Codeforces 344C Rational Resistance的更多相关文章

  1. [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 ...

  2. Codeforces Round #200 (Div. 1)A. Rational Resistance 数学

    A. Rational Resistance Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  3. Codeforces Round #200 (Div. 2) C. Rational Resistance

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. codeforces 200 div2 C. Rational Resistance 思路题

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. codeforces343A A. Rational Resistance

    http://http://codeforces.com/problemset/problem/343/A A. Rational Resistance time limit per test 1 s ...

  6. CodeForces Round 200 Div2

    这次比赛出的题真是前所未有的水!只用了一小时零十分钟就过了前4道题,不过E题还是没有在比赛时做出来,今天上午我又把E题做了一遍,发现其实也很水.昨天晚上人品爆发,居然排到Rank 55,运气好的话没准 ...

  7. Codeforces Round #200 (Div. 1 + Div. 2)

    A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...

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

    1229: Rational Resistance Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 8  Solved: 4 [id=1229" ...

  9. CF 200 div.1 A

    2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...

随机推荐

  1. C#之改变窗体icon图标、新建类文件、调用dll库

    一.改变窗体的图标 没有修改之前为: 修改之后为自己想要的图标: 需要在窗体Form1.cs属性里边添加icon图片文件: 二.新建cs类文件 如下图所示,新建一个类文件,我用于来调用库文件也可以来定 ...

  2. __weak修饰符

    前言 在 HAL 库中,很多回调函数前面使用__weak 修饰符. weak 顾名思义是“弱”的意思,所以如果函数名称前面加上__weak 修饰符,我们一般称这个函数为“弱函数”. 加上了__weak ...

  3. jquery IE7 下报错:SCRIPT257: 由于出现错误 80020101 而导致此项操作无法完成

        非IE(内核)浏览器运行正常,在IE中运行异常,一般考虑为js中多了符号.     常见的有:         1.上面的html注释"<!-- -->",这种 ...

  4. gps 地图

    http://www.cnblogs.com/sylvanas2012/p/5342530.html http://blog.csdn.net/ma969070578/article/details/ ...

  5. NHibernate概括

    什么是?NHibernate?NHibernate是一个面向.NET环境的对象/关系数据库映射工具. 对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种 ...

  6. Domino 使用递归算法获取视图值

    在关系数据库中,有两字段是父子关系.通过递归算法,输入一个父ID,能够获取全部相应的子ID.这种数据结构在组织架构中常常使用.显示一般使用树形结构.在Domino中相同能够处理这种情况,下面是个小de ...

  7. iOS 中client和server的 Web Service 网络通信 (1)

    当你打开你手机上新浪微博应用或者知乎应用是.你是否会去想这些显示在手机上的图片和数据时从哪里来的?又是通过如何的方法实现的?好.那么接下来就介绍是如何实现的.过程又是怎么样的.      当我们浏览着 ...

  8. H5学习_番外篇_PHP数据库操作

    1. 文件操作 1.1 打开关闭文件 fopen() resource fopen ( string filename, string mode [, bool use_include_path [, ...

  9. DNS SOA NS区别

    转自 http://bbs.51cto.com/thread-908637-1.html NS服务器里有两个比较重要的记录.一个叫SOA记录(起始授权机构) 一个叫NS(Name Server)记录( ...

  10. 转】关于cgi、FastCGI、php-fpm、php-cgi

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...