codeforces343A A. Rational Resistance
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:
- one resistor;
- an element and one resistor plugged in sequence;
- 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 caseRe 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.
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.
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 thecin,
cout streams or the%I64d specifier.
1 1
1
3 2
3
199 200
200
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欧姆的电阻,问最少用多少个电阻可以等效成a/b欧姆;
注意用__int64;
分析:这个题有个规律,就是a/b,b/a所需要的电阻一样,只是串并联关系不一样而已,因此该题可以这样考虑:写成假分子的形式a/b,(a>b)取整数部分,然后对剩余的电阻a1/b1进行类似的运算(a1>b1)
知道a/b可以除尽位置sum记录的整数值之和就是答案:
程序:
#include"string.h"
#include"stdio.h"
int main()
{
__int64 a,b,p,t;
while(scanf("%I64d%I64d",&a,&b)!=-1)
{
__int64 sum=0;
while(1)
{
if(a<b)
{
t=a;
a=b;
b=t;
}
p=a/b;
sum+=p;
if(a%b==0)
break;
a-=b*p;
}
printf("%I64d\n",sum);
}
}
codeforces343A A. Rational Resistance的更多相关文章
- Codeforces Round #200 (Div. 1)A. Rational Resistance 数学
A. Rational Resistance Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...
- 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 ...
- codeforces 200 div2 C. Rational Resistance 思路题
C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 344C Rational Resistance
Description Mad scientist Mike is building a time machine in his spare time. To finish the work, he ...
- [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 ...
- CodeForces Round 200 Div2
这次比赛出的题真是前所未有的水!只用了一小时零十分钟就过了前4道题,不过E题还是没有在比赛时做出来,今天上午我又把E题做了一遍,发现其实也很水.昨天晚上人品爆发,居然排到Rank 55,运气好的话没准 ...
- zzu--2014年11月16日月潭赛 B称号
1229: Rational Resistance Time Limit: 1 Sec Memory Limit: 128 MB Submit: 8 Solved: 4 [id=1229" ...
- CF 200 div.1 A
2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces Round #200 (Div. 1 + Div. 2)
A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...
随机推荐
- Python在windows下的安装与配置
安装python 文件准备: A. python安装文件:我用的是python-3.4.3.amd64.msi: 安装很简单,直接双击点下一步即可: 配置环境变量,在windows系统变量中找到pat ...
- ubuntu挂载其他分区到/home下,将当前分区内容替换
有时候,我们装系统时,可能因为没注意,把某一个分区分小了,导致到最后,我们的那个盘容不下了, 这时,面临的两个选择就是:要么卸载一些软件,要么重新分区,重装系统,其实,还可以这样,去把其他 多余的盘分 ...
- Python之 for循环\while循环
list或tuple可以表示一个有序集合.如果我们想依次访问一个list中的每一个元素呢?比如 list: L = ['Adam', 'Lisa', 'Bart'] print L[0] print ...
- Bootstrap 表单和图片 (内联表单,表单合组,水平排列,复选框和单选框,下拉列表,校验状态,添加额外的图标,控制尺寸,图片)
一.表单 基本格式 注:只有正确设置了输入框的 type 类型,才能被赋予正确的样式. 支持的输入框控件 包括:text.password.datetime.datetime-local.date.m ...
- 蓝牙的AVCTP协议笔记
1.概述 AVCTP协议描述了蓝牙设备间Audio/Video的控制信号交换的格式和机制,它是一个总体的协议,具体的控制信息由其指定的协议(如AVRCP)实现,AVCTP本身只指定控制comm ...
- crucible3.x +fisheye3.x 安装和破解
2015-11-24 22:29 423人阅读 评论(1) 收藏 举报 分类: linux(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. 破解文件下载路径:http://downlo ...
- jfinal
http://blog.csdn.net/zb0567/article/details/21083021
- 【Android开发学习笔记】【第八课】五大布局-下
概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...
- 规则html表单对象赋值
function grid_load_callback(data, status) { if (data.rows.length > 0) { ...
- DOM、SAX、JDOM、DOM4J四种XML解析方法PK
基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式 --优点: (1)形成了树结构,直观好理解,代码更易编写 ...