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. 自己的第一个android应用(天气)

    主界面代码 package com.example.weather; import android.os.Bundle; import android.app.Activity; import and ...

  2. Studious Student Problem Analysis

    (http://leetcode.com/2011/01/studious-student-problem-analysis.html) You've been given a list of wor ...

  3. pthread 简要使用指南

    http://www.2cto.com/kf/201208/151995.html http://www.2cto.com/kf/201208/151997.html

  4. 关于jq操作table下多个type=radio的input的选中

    假如有2个table: <table id="table1" border="0"> <tr> <td><input ...

  5. win7 资源管理器的背景色修改

    主要参考 http://blog.sina.com.cn/s/blog_49c182c20100w3nb.html win7 通过dll修改背景色首先找到这个文件C:\Windows\Resource ...

  6. 帝国cms7.0导航栏当前栏目显示不同样式

    以下代码演示帝国导航调用 栏目id为14.15.2.34的栏目内容.同时加入php语句,如果栏目显示为当前栏目,则变量<?=$on?> 显示为 an ,否则就显示为空 [e:loop={& ...

  7. 手机SIM卡知识大科普

    SIM卡 SIM卡是(Subscriber Identity Module 客户识别模块)的缩写,也称为智能卡.用户身份识别卡,GSM数字移动电话机必须装上此卡方能使用.它在一电脑芯片上存储了数字移动 ...

  8. (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示

    原文 (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows She ...

  9. FastJson中@JSONField注解使用

    最近做项目中,使用了json格式在服务器之间进行数据传输.但是发现json格式数据不符合JAVA中的变量定义规则,并且难以理解,因此需要在后台中做二次处理,将数据处理成我们系统中定义的格式. 思路: ...

  10. 一个php user class

    这个类叫php user class.php user class is an easy to use php snippet for user manipulation (register, log ...