2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】
任意门: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
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 a0, a1, a2, ... are integers and a1, a2, ... > 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.
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.
For each test case, display the case number followed by the continued fraction representation of r1 + r2, r1- r2, r1×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).
4 3
5 1 1 2
5 2 2
0 0
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 【模拟连分数】的更多相关文章
- Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)
A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...
- 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 ...
- 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 ...
- [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 ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- 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 ...
- East Central North America Region 2015
E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...
- poj 2732 Countdown(East Central North America 2005)
题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...
- 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. ...
随机推荐
- Spring 和整个体系 @Value注解 配合属性文件.property
未来学习方向 重要思路 学的时候看官方文档,系统地学,比如学Spring Boot ,但真正使用的时候,有比自动化(条件化)配置,约定即配置 更好的方法(这个可读性不强,对电脑来说它执行的代码一样 ...
- vue中src下的assets文件与static文件的几点区别
区别一: assets文件时src下的,所以最后运行时需要进行打包:而static文件不需要打包就直接放在最终的文件中了. 区别二: assets中的文件在.vue中的template/style下用 ...
- 把数据库内容显示在listview上
数据库操作很简单,但用户想看见的是数据库里的内容,那么让数据库内容显示在屏幕上呢,下面做个简单演示,百变不离其中,先看步骤: 把数据库的数据显示至屏幕1. 任意插入一些数据 定义Javabean:Pe ...
- hibernateAPI详解
1 Configuration package www.test.b_api; import org.hibernate.Session; import org.hibernate.SessionFa ...
- 数组和json的相互转换
json_encode() <?php /*****一维数组*********/ //有键 $arr = array( 'a'=>1, 'b'=>2, 'c'=>3, ); $ ...
- pat09-散列3. Hashing - Hard Version (30)
09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...
- jquery获取元素与屏幕高度距离
a. onscroll事件 scroll是css样式中overflow的一个值,意思是显示滚动条;当一个元素的实际高度超过他的最大高度是,只要设置了overflow为scroll b. $(..).s ...
- SpringSecurity 3.2入门(2)环境搭建
由于目前Spring官方只提供Meven的下载方式,为了能以最快的速度入门使用框架,这里提供百度网盘下载链接. 注:本入门教程默认已经配置成功SpringMVC框架. 1.web.xml配置 < ...
- Spring Chapter4 WebSocket 胡乱翻译 (一)
4. WebSocket 包含了Servlet stack,原生WebSocket交互,通过SockJS模拟,并且通过STOMP在WebSocket之上订阅.发布消息. 4.1 简介 不扯了,看到这个 ...
- 在MyEclipse中使用javadoc导出API文档详解
本篇文档介绍如何在MyEclipse中导出javadoc(API)帮助文档,并且使用htmlhelp.exe和jd2chm.exe生成chm文档. 具体步骤如下: 打开MyEclipse,选中想要制作 ...