Codeforces 344C Rational Resistance
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:
- 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 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
1 1
1
3 2
3
199 200
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的更多相关文章
- [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 (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 ...
- codeforces343A A. Rational Resistance
http://http://codeforces.com/problemset/problem/343/A A. Rational Resistance time limit per test 1 s ...
- CodeForces Round 200 Div2
这次比赛出的题真是前所未有的水!只用了一小时零十分钟就过了前4道题,不过E题还是没有在比赛时做出来,今天上午我又把E题做了一遍,发现其实也很水.昨天晚上人品爆发,居然排到Rank 55,运气好的话没准 ...
- Codeforces Round #200 (Div. 1 + Div. 2)
A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...
- 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 ...
随机推荐
- 普通码农和CTO之间的差距
虚心 学习的第一步是--"我不懂".一个空是水杯才能装水,如果是满的就没有办法装水了."自我肯定"是一种非常难克服的习惯,经常会有朋友看到某个技术或者实现之后不 ...
- 【转】C# 正则表达式大全
[转]C# 正则表达式大全 前言 在网上看到一个不错的简易版正则匹配和替换的工具,现在补充进来,感觉还不错,效果如下(输入验证中文汉字的正则表达式) 在线下载 密码:5tpt 注:好像也是一位园友 ...
- What's Wrong With Hue Oozie Editor?
本文原文出处: http://blog.csdn.net/bluishglc/article/details/47021019 严禁不论什么形式的转载,否则将托付CSDN官方维护权益! First, ...
- Effective JavaScript Item 49 对于数组遍历,优先使用for循环,而不是for..in循环
本系列作为Effective JavaScript的读书笔记. 对于以下这段代码,能看出最后的平均数是多少吗? var scores = [98, 74, 85, 77, 93, 100, 89]; ...
- 前端到后台ThinkPHP开发整站--php开发案例
前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...
- linux下修改完profile文件的环境变量后如何立即生效
方法1: 让/etc/profile文件修改后立即生效 ,可以使用如下命令: # . /etc/profile 注意: . 和 /etc/profile 有空格 方法2: 让/etc/profile文 ...
- QuerySet和对象的例子 个人记录
import osif __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE&quo ...
- POJ 1631 nlogn求LIS
方法一: 二分 我们可以知道 最长上升子序列的 最后一个数的值是随序列的长度而递增的 (呃呃呃 意会意会) 然后我们就可以二分找值了(并更新) //By SiriusRen #include < ...
- powerdesigner里的table背景色是不是可以修改的?
Tools->Display Preferences->Format->Table->Modify->Fill->Fill color:
- Busy waiting
In computer systems organization or operating systems, "busy waiting" refers to a process ...