/*
Stern-Brocot代数系统 Stern-Brocot树是一种生成所有非负的最简分数m/n的美妙方式。
其基本方式是从(0/1, 1/0)这两个分数开始, 根据需要反复执行如下操作:
在相邻的分数 m/n 和 m1/n1之间插入 (m+m1)/(n+n1) 例如,可以将它看成是无限延伸的二叉树
0 1 1
--- --- ---
1 1 0
/ \
/ \
1 2
--- ---
2 1
/ \ / \
/ \ / \
1 2 3 3
--- --- --- ---
3 3 2 1
.... ... .......
用字母 L 和 R 分别表示从树根开始的一步 往左走 和 往右走,
则一个L和R组成的序列唯一确定了树中的一个位置。 唯一例外的是 1/1.
*/ #include<iostream>
#include<string>
#include<vector>
using namespace std; typedef struct Point
{
int x;
int y;
const Point& operator+=(const Point& pt)
{
x+=pt.x;
y+=pt.y;
return *this;
}
}LP,RP; void getStr(LP leftP, RP rightP, Point pt, const Point P, string& str)
{
if(pt.x == P.x && pt.y==P.y)
{
return;
}
double tmp = pt.x*1.0/pt.y;
double tmp1 = leftP.x*1.0/leftP.y;
double tmp2 = P.x*1.0/P.y;
double tmp3;
if(rightP.y==0)
{
tmp3 = INT_MAX*0.1;
}
else
{
tmp3 = rightP.x*1.0/rightP.y;
}
if(tmp2<tmp&&tmp2>tmp1)
{
rightP = pt;
pt+=leftP; str+='L';
getStr(leftP, rightP, pt, P, str);
}
if(tmp2>tmp&&tmp2<tmp3)
{
leftP = pt;
pt+=rightP; str+='R';
getStr(leftP, rightP, pt, P, str);
}
} int main()
{
int M, N;
vector<string> svec;
string str; Point leftP, rightP, Pt, P;
while(cin>>M>>N)
{
if(1==M&&1==N)
break;
str.clear();
P.x = M;
P.y = N;
leftP.x = 0;
leftP.y = 1;
rightP.x = 1;
rightP.y = 0;
Pt.x = 1;
Pt.y = 1;
getStr(leftP, rightP, Pt, P, str);
svec.push_back(str);
} for (int i=0; i<svec.size(); i++)
{
cout<<svec[i]<<endl;
} return 0;
} /*
5 7
878 323
1 1 */

  

挑战编程PC/UVa Stern-Brocot代数系统的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  3. CSDN挑战编程——《数学问题》

    数学问题 题目详情: 给你两个长度为n的正整数序列分别为{a1,a2,a3...an},{b1,b2,b3...bn},0<ai,bi<=100: 设S=max{x1*a1+x2*a2+x ...

  4. CSDN挑战编程——《绝对值最小》

    绝对值最小 题目详情: 给你一个数组A[n],请你计算出ans=min(|A[i]+A[j]|)(0<=i,j<n). 比如:A={1, 4, -3}, 则: |A[0] + A[0]| ...

  5. 挑战编程 uva100 3n+1

    挑战编程 刘汝佳 的第一道习题  热身题 熟悉下提交格式 题意 #include <iostream> #include <algorithm> using namespace ...

  6. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版

    , '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...

  7. The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  8. CSDN挑战编程——《金色十月线上编程比赛第二题:解密》

    金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...

  9. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

随机推荐

  1. 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)

    Laravel  和 Laravel  的邮件发送使用方式完全一致.Laravel  的邮件发送中文文档在:http: 邮箱为例,展示如何用 Laravel 内置的邮件发送类来发送邮件. 配置 修改邮 ...

  2. hdu 4165 dp

    可以用卡特兰数做 以下分析转自:http://www.cnblogs.com/kevinACMer/p/3724640.html?utm_source=tuicool 这道题之前自己做的时候并没有反应 ...

  3. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  4. Android 生成和Pull解析xml

    一.单个对象生成xml 生成以下xml,该怎么生成呢? <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <accou ...

  5. 使用iterator出现的死循环

    public static void main(String[] args) { List<String> list = new ArrayList<String>(); li ...

  6. 参加 TiD 2015 是怎样一番体验?

    人生有很多第一次,(专程打飞的去帝都)参加软件大会,我也是第一次.   TiD   说到软件大会,QCon.PyCon 什么的早已如雷贯耳,没吃过猪肉还没见过猪跑?但对于 TiD,确实还是头一次听说. ...

  7. HDU 2853 & 剩余系+KM模板

    题意: 给你一张二分图,给一个原匹配,求原匹配改动最少的边数使其边权和最大. SOL: 我觉得我的智商还是去搞搞文化课吧..这种题给我独立做我大概只能在暴力优化上下功夫.. 这题的处理方法让我想到了剩 ...

  8. Zepto源码注释

    /* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...

  9. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

  10. 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq(线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1798 之前写了个快速乘..........................20多s...... 还好 ...