C. Rational Resistance
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
1 1
output
1
input
3 2
output
3
input
199 200
output
200
Note

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.

题意:要得到a/b的电阻最小需要多少个电阻;(注意:每次只能串联一个或者并联一个);

思路:每次串联一个得到(a+b)/a,并联一个得到a/(a+b);

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define inf 100000000000005
#define MAXN 10000010
//#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
ll x,y,z,i,t;
scanf("%I64d%I64d",&x,&y);
ll ans=;
while()
{
if(x>y)
{
ans+=x/y;
x%=y;
if(x==)
break;
}
else if(x<y)
{
z=x;
x=y-x;
y=z;
ans++;
}
else
{
ans+=;
break;
}
}
cout<<ans<<endl;
return ;
}

codeforces 200 div2 C. Rational Resistance 思路题的更多相关文章

  1. Bakery CodeForces - 707B (最短路的思路题)

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...

  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 Round 200 Div2

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

  5. codeforces #262 DIV2 B题 Little Dima and Equation

    题目地址:http://codeforces.com/contest/460/problem/B 这题乍一看没思路.可是细致分析下会发现,s(x)是一个从1到81的数,不管x是多少.所以能够枚举1到8 ...

  6. codeforces343A A. Rational Resistance

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

  7. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  8. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  9. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

随机推荐

  1. Windows Phone 几种页面间传递数据的方式

    首先,我们要引用:using Microsoft.Phone.Shell; 第一种: // 导航到新页面 NavigationService.Navigate(new Uri("/Detai ...

  2. 微信小程序 --- model弹框

    model弹框:在屏幕中间弹出,让你进行选择: 效果: 代码: <button type="primary" bindtap="btnclick"> ...

  3. Linux环境下安装Redis并完成测试(已验证)

    准备工作: 本人测试环境:Win7 xshell远程登录Linux Linux: ubuntu 软件包:redis-3..2.6.tar.gz (Linux下redis安装包) =========== ...

  4. zabbix 部署 jmx 监控tomcat

    zabbix提供了一个java gateway的应用去监控jmx(Java Management Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架.JMX可以 ...

  5. SQL---->mySQl查看和更改端口

    修改端口: 采用dmg方式安装的mysql,默认启动端口为3307,不是默认的3306.如果想改为3306,可以编辑 /Library/LaunchDaemons     /com.Oracle.os ...

  6. Python爬虫框架Scrapy实例(二)

    目标任务:使用Scrapy框架爬取新浪网导航页所有大类.小类.小类里的子链接.以及子链接页面的新闻内容,最后保存到本地. 大类小类如下图所示: 点击国内这个小类,进入页面后效果如下图(部分截图): 查 ...

  7. Spark-RDD算子

    一.Spark-RDD算子简介 RDD(Resilient Distributed DataSet)是分布式数据集.RDD是Spark最基本的数据的抽象. scala中的集合.RDD相当于一个不可变. ...

  8. Spark集群安装和WordCount编写

    一.Spark概述 官网:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 为大数据处理而设计的快速通用的计算引擎. Spark加州大学 ...

  9. MongoDB Windows环境安装及配置( 一)

    原文http://www.cnblogs.com/lzrabbit/p/3682510.html MongoDB一般安装 1.首先到官网 (http://www.mongodb.org/downloa ...

  10. EOS 的网站及资料doc

    https://github.com/EOSIO/Documentation/blob/master/zh-CN/Roadmap.md https://bytemaster.github.io/bit ...