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.

Sample test(s)
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.

水题,可 以转化成子问题!如果,是大于1,取整部分当成k个串联电阻,如果小于1部分,倒过来,重复上过程 ,一定可以得到结果!

#include <iostream>
#pragma comment(linker,"/STACK:10240000000,10240000000")
#include <stdio.h>
#include <string.h>
using namespace std; __int64 gcd(__int64 a,__int64 b)
{
if(a==0)return b;
return gcd(b%a,a);
}
__int64 ff(__int64 a,__int64 b)
{
__int64 answer=0,temp;
while(a&&b)
{
__int64 c=gcd(a,b);
if(c)
a=a/c,b=b/c;__int64 k=a/b;answer+=k;temp=b;b=a-b*k;a=temp;
}
return answer;
}
int main()
{
__int64 a,b; while(scanf("%I64d%I64d",&a,&b)!=EOF){
__int64 c=ff(a,b);
printf("%I64d\n",c);
}
return 0;
}

Codeforces Round #200 (Div. 2) C. Rational Resistance的更多相关文章

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

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

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

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

  3. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  4. Codeforces Round #200 (Div. 1) C. Read Time 二分

    C. Read Time Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/C ...

  5. Codeforces Round #200 (Div. 1) B. Alternating Current 栈

    B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  6. Codeforces Round #200 (Div. 1) BCD

    为了锻炼个人能力奋力div1 为了不做原题从200开始 B 两个电线缠在一起了 能不能抓住两头一扯就给扯分开 很明显当len为odd的时候无解 当len为偶数的时候 可以任选一段长度为even的相同字 ...

  7. Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)

    D. Alternating Current time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  9. Codeforces Round #200 (Div. 2) E. Read Time(二分)

    题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...

随机推荐

  1. js页面loading加载

    <html>     <head>     <title>页面正在载入</title>       <script  language=" ...

  2. MD5随机散列加密算法

    项目中需要在登录验证用户名.密码的时候对密码进行加密处理,由于是比较商业化的软件,所以安全方面还是必须要考虑的.而使用MD5随机散列加密算法使得密码加密后不可逆,很大程度上提升了安全性.废话不多说,看 ...

  3. 浅谈JSP(一)

    一.JSP引言 JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计.它是在传统的网页HTML文件(*.htm,*.html)中插入Java ...

  4. WPF多线程下载文件,有进度条

    //打开对话框选择文件         private void OpenDialogBox_Click(object sender, RoutedEventArgs e)         {     ...

  5. C#_C++_SDK_WM_KEYDOWN人物卡顿延迟解决方法

    提问者采纳 由Keydown和keyup事件组合,keyDown来判定按下,此时开始移动,KeyUp判定松开,这样可行否? 追问 这是我一开始的写法,但就是因为 键盘重复延迟 导致keydown后会有 ...

  6. python自学笔记(九)python练习题

    1. 已知字符串 a = "aAsmr3idd4bgs7Dlsf9eAF",要求如下 1.1 请将a字符串的大写改为小写,小写改为大写 print a.swapcase() 1.2 ...

  7. mybatis foreach where test用法

    <select id="selectAny" resultType="user" parameterType="user"> s ...

  8. .net mvc Authorization Filter,Exception Filter与Action Filter

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  9. PC游戏编程(入门篇)(前言写的很不错)

    PC游戏编程(入门篇) 第一章 基石 1. 1 BOSS登场--GAF简介 第二章 2D图形程式初体验 2.l 饮水思源--第一个"游戏"程式 2.2 知其所以然一一2D图形学基础 ...

  10. 带你一起Piu Piu Piu~

    单刀直入,今天要讲的是自己写的一个WPF动画例子.我们在看下最终效果~ 最近在重看WPF编程宝典2010,在练习第15章动画性能例子时有了些想法.原始例子如下:  原始例子(打包了整个15章的) 它是 ...