任意门: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. 《The Python Tutorial》——Errors and Exceptions 阅读笔记

    Errors and Exceptions 官方文档:https://docs.python.org/3.5/tutorial/errors.html python中所有的异常都继承自BaseExce ...

  2. D. Match & Catch 后缀自动机 || 广义后缀自动机

    http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...

  3. Java基础22-Static关键字

    1.static关键字 public class Test{ public static void main(String[] args){ Persion p1=new Persion(); Per ...

  4. 阿里云服务器对外开放tomcat端口访问

    今天第一次在阿里云服务器ecs上安装完成tomcat,然后启动tomcat之后.在本地输入ip:端口,发现不能访问. 出现这个的原因可能是你购买的服务器是 专有网络 类型的 如果是专有网络类型的服务器 ...

  5. jquery返回顶部和底部插件和解决ie6下fixed插件

    (function($){ //返回顶部和底部插件 $.fn.extend({ goTopBootom:function (options){ //默认参数 var defaults = { &quo ...

  6. netstat参数

    1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接.路由表.接口状态链接.多播成员等等. 定义:Netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TC ...

  7. dpkg: error: dpkg status database is locked by another process 解决方法

    使用dpkg -i/apt命令安装,报错: ------------------------------------------------------------- dpkg: error: dpk ...

  8. 洛谷P1966 火柴排队(逆序对)

    题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...

  9. Stage1--Python的特点和安装

    说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...

  10. Apache Flume

    An Event is a unit of data that flows through a Flume agent. The Event flows from Source to Channel  ...