任意门:http://codeforces.com/gym/100641/attachments

Con + tin/(ued + Frac/tions)

  • Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • Total Submission(s): 217     Accepted Submission(s): 27
Description

The (simple) continued fraction representation of a real number r is an expression obtained by an iterative process of representing r as a sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on. In other words, a continued fraction representation of r is of the form

where a0a1a2, ... are integers and a1a2, ... > 0. We call the ai-values partial quotients. For example, in the continued fraction representation of 5.4 the partial quotients are a0 = 5, a1 = 2, and a2 = 2. This representation of a real number has several applications in theory and practice.

While irrational numbers like √2 (sqrt(2)) require an infinite set of partial quotients, any rational number can be written as a continued fraction with a unique, finite set of partial quotients (where the last partial quotient is never 1 in order to preserve uniqueness). Given two rational numbers in continued fraction representation, your task is to perform the four elementary arithmetic operations on these numbers and display the result in continued fraction representation.

Input

Each test case consists of three lines. The first line contains two integers n1 and n2, 1 ≤ ni ≤ 9 specifying the number of partial quotients of two rational numbers r1 and r2. The second line contains the partial quotients of r1 and the third line contains the partial quotients of r2. The partial quotients satisfy |a0| ≤ 10 and 0 < ai ≤ 10, the last partial quotient will never be 1, and r2 is non-zero. A line containing two 0's will terminate input.

Output

For each test case, display the case number followed by the continued fraction representation of r1 + r2r1r2r1×r2, and r1/r2 in order, each on a separate line. Use 64-bit integers for all of your calculations (long long in C++ and long in Java).

Sample Input

4 3
5 1 1 2
5 2 2
0 0

Sample Output

Case 1:
11
0 5
30 4 6
1 27

题意概括:

给一串 a 按照连分数定义算出 r1;给一串 b 按照连分数定义算出 r2;

最后按照连分数的格式 输出 r1 + r2, r1- r2, r1 * r2, r1 / r2;

解题思路:

模拟一遍连分数计算过程算出 r1 和 r2;

然后判断 r1 r2 是分数还是整数(因为是 LL ,作除法时对精度有要求)

模拟求连分数的逆过程输出 r1 r2 四则运算的结果。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define LL long long
using namespace std;
const int MAXN = ;
LL x1, x2, y, y2;
LL A[MAXN], B[MAXN];
int N, M; LL Gcd(LL a, LL b)
{
if(b == ) return a;
return Gcd(b, a%b);
} void solve1() //求 r1 分数
{
LL t = ;
x1 = , y = A[N-];
for(int i = N-; i > ; i--){
x1 = x1+A[i]*y;
t = y;
y = x1;
x1 = t;
}
x1 = x1+A[]*y;
} void solve2() //求 r2 分数
{
LL t = ;
x2 = , y2 = B[M-];
for(int i = M-; i > ; i--){
x2 = x2+B[i]*y2;
t = y2;
y2 = x2;
x2 = t;
}
x2 = x2+B[]*y2;
} void ppt(LL a, LL b) //输出连续分式(求分数的逆过程)
{
LL gg = Gcd(a, b);
a /= gg;
b /= gg;
if(b < ){ //分母小于零时,模不小于零
a*=-;
b*=-;
}
LL tmp = (a%b+b)%b; //小数部分
LL a0 = (a-tmp) / b; //整数部分
printf("%lld", a0);
a = tmp; //处理剩下的小数部分 swap(a, b); //分子分母倒置
while(b){
tmp = a/b;
printf(" %lld", tmp);
a=a%b;
swap(a, b);
}
puts("");
} int main()
{
int T_case = ;
while(~scanf("%d%d", &N, &M) && (N+M)){
for(int i = ; i < N; i++){
scanf("%lld", &A[i]);
}
for(int i = ; i < M; i++){
scanf("%lld", &B[i]);
}
if(N == ) x1 = A[], y = ;
else solve1();
if(M == ) x2 = B[], y2 = ;
else solve2(); printf("Case %d:\n", ++T_case);
//加法 if(y == || y2 == ){ //存在一个整数
if(y != && y2 == ){ //r2 为整数
ppt(x2*y+x1, y);
}
else if(y2 != && y == ){ //r1 为整数
ppt(x1*y2+x2, y2);
}
else{ //两个都是整数
printf("%lld\n", x1+x2);
}
}
else{ //两个都是分数
ppt(x1*y2+x2*y, y*y2);
} //减法 if(y == || y2 == ){ //存在一个整数
if(y != && y2 == ){ //r2 为整数
ppt(x1-x2*y, y);
}
else if(y2 != && y == ){ //r1 为整数
ppt(x1*y2-x2, y2);
}
else{ //两个都是整数
printf("%lld\n", x1-x2);
}
}
else{ //两个都是分数
ppt(x1*y2-x2*y, y*y2);
} //乘法 if(y == || y2 == ){ //存在一个整数
if(y != && y2 == ){ //r2 为整数
ppt(x2*x1, y);
}
else if(y2 != && y == ){ //r1 为整数
ppt(x1*x2, y2);
}
else{ //两个都是整数
printf("%lld\n", x1*x2);
}
}
else{ //两个都是分数
ppt(x1*x2, y*y2);
} //除法 if(y == || y2 == ){ //存在一个整数
if(y != && y2 == ){ //r2 为整数
ppt(x1, x2*y);
}
else if(y2 != && y == ){ //r1 为整数
ppt(x1*y2, x2);
}
else{ //两个都是整数
ppt(x1, x2);
}
}
else{ //两个都是分数
ppt(x1*y2, y*x2);
}
}
return ;
}

2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】的更多相关文章

  1. Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

    A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...

  2. 2017-2018 ACM-ICPC East Central North America Regional Contest (ECNA 2017) Solution

    A:Abstract Art 题意:给出n个多边形,求n个多边形分别的面积和,以及面积并 思路:模板 #include <bits/stdc++.h> using namespace st ...

  3. 2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp

    Problem F Removal GameBobby Roberts is totally bored in his algorithms class, so he’s developed a li ...

  4. [bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation

    Problem D Lost in Translation The word is out that you’ve just finished writing a book entitled How ...

  5. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

  6. ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010

    ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...

  7. East Central North America Region 2015

    E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...

  8. poj 2732 Countdown(East Central North America 2005)

    题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...

  9. POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

    题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...

随机推荐

  1. spark第三篇:Cluster Mode Overview 集群模式预览

    Spark applications run as independent sets of processes on a cluster, coordinated by the SparkContex ...

  2. postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改

    如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...

  3. Activemq API使用(不整合spring)

    首先需要引入activemq的jar包,这里用的是5.14.4版本的 <!-- https://mvnrepository.com/artifact/org.apache.activemq/ac ...

  4. SQL Server 2008 SQL2012 SQL2014 收缩日志 清空删除大日志文件

    SQL2008 SQL2012 SQL2014 的收缩日志 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Bac ...

  5. win7与ubuntu双系统安装

    机器型号:联想V470 对系统引导一直不是很明白,导致我出现几次失败. 一直挺喜欢ubuntu的,因为,第一我感觉它比较友好,第二我初次接触linux就是ubuntu,当初还是同学帮助我wubi进行安 ...

  6. STL:set用法总结

    一:介绍 set是STL的关联式容器,以红黑树(Red-Black Tree)作为底层数据结构.自动去重,保证每个元素唯一,并对数据进行排序. 命名空间为std,所属头文件为<set> 二 ...

  7. 使用jxl读取excel内容,并转换成Json,用于Datagrid

    一.上传excel文件,得到InputStream,由InputStream得到Jxl中的Workbook,取出内容,存到二维数组中. 1.使用 Jquery Uploadify 插件(http:// ...

  8. 读《Wireshark网络分析就这么简单》读书笔记

    晚上花了两个多小时看完这本书,记录下一些看书过程中的笔记. 一.问题:A和B 是否能正常通信? 两台服务器A和服务器B的网络配置 A                                  ...

  9. Spring mvc框架下使用kaptcha生成验证码

    1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...

  10. (五)html部分标签元素补充

    html标签元素十分的多,一次性掌握全部,那是不可能的,在后续的学习中,会不断补充标签元素... 1.行元素和块元素 行标签元素即是标签元素根据内容大小进行自适应,而不是占据一整行. 如<spa ...