Continued Fractions CodeForces - 305B (java+高精 / 数学)
A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as
and the other one is represented as a finite fraction of height n. Check if they are equal.
Input
The first line contains two space-separated integers p, q (1 ≤ q ≤ p ≤ 1018) — the numerator and the denominator of the first fraction.
The second line contains integer n (1 ≤ n ≤ 90) — the height of the second fraction. The third line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1018) — the continued fraction.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print "YES" if these fractions are equal and "NO" otherwise.
Examples
9 4
2
2 4
YES
9 4
3
2 3 1
YES
9 4
3
1 2 4
NO
Note
In the first sample .
In the second sample .
In the third sample .
思路:
可以用java的高精度类BigDecimal直接暴力模拟分式的递归运算过程,精度保留到30以上均可以AC
我的JAVA代码:
import java.math.*;
import java.util.Scanner;
public class Main { static long a[] = new long[500];
public static int n;
public static BigDecimal f(int index)
{
if(index==n)
return BigDecimal.valueOf(a[index]).divide(BigDecimal.ONE,45,BigDecimal.ROUND_HALF_DOWN);
else
return BigDecimal.valueOf(a[index]).add(BigDecimal.ONE.divide(f(index+1),45,BigDecimal.ROUND_HALF_DOWN));
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigDecimal p,q; p=cin.nextBigDecimal();
q=cin.nextBigDecimal();
n=cin.nextInt();
for(int i=1;i<=n;i++)
{
a[i]=cin.nextLong();
}
BigDecimal s1=p.divide(q,45,BigDecimal.ROUND_HALF_DOWN);
BigDecimal s2=f(1);
// System.out.println(s1);
// System.out.println(s2);
if(s1.equals(s2))
{
System.out.println("YES");
}else
{
System.out.println("NO");
} } }
还有C++数学解法:
把它上下翻转一下呢?
这样迭代下去,如果是相等的,那么右边一定是等于0的.
图来自这位大佬的博客:
https://blog.csdn.net/theArcticOcean/article/details/50429314
注意:
中间特判下分母为0的情况和中途出结果的情况
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-16
#define eps2 1e-15
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll p,q;
int n;
ll a[maxn];
int main()
{
dll(p);dll(q);
gg(n);
repd(i,,n)
{
dll(a[i]);
}
int flag=;
repd(i,,n)
{
if(q==||(p*1.0000000/q)<a[i])
{
flag=;
break;
}else
{
p-=q*a[i];
swap(p,q);
}
}
if(flag==&&q==)
{
printf("YES\n");
}else
{
printf("NO\n");
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Continued Fractions CodeForces - 305B (java+高精 / 数学)的更多相关文章
- CF 305B——Continued Fractions——————【数学技巧】
B. Continued Fractions time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- java高级精讲之高并发抢红包~揭开Redis分布式集群与Lua神秘面纱
java高级精讲之高并发抢红包~揭开Redis分布式集群与Lua神秘面纱 redis数据库 Redis企业集群高级应用精品教程[图灵学院] Redis权威指南 利用redis + lua解决抢红包高并 ...
- jzoj6005. 【PKUWC2019模拟2019.1.17】数学 (生成函数+FFT+抽代+高精)
题面 题解 幸好咱不是在晚上做的否则咱就不用睡觉了--都什么年代了居然还会出高精的题-- 先考虑如果暴力怎么做,令\(G(x)\)为\(F(n,k)\)的生成函数,那么不难发现\[G^R(x)=\pr ...
- bzoj 3287: Mato的刷屏计划 高精水题 && bzoj AC150
3287: Mato的刷屏计划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 124 Solved: 43[Submit][Status] Desc ...
- BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
题目链接 BZOJ5300 题解 这题真的是很丧病,,卡高精卡到哭 我们设\(f[i]\)表示卸掉前\(i\)个环需要的步数 那么 \[f[i] = 2*f[i - 2] + f[i - 1] + 1 ...
- 洛谷1601 A+B Problem(高精) 解题报告
洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...
- zz高精地图和定位在自动驾驶的应用
本次分享聚焦于高精地图在自动驾驶中的应用,主要分为以下两部分: 1. 高精地图 High Definition Map 拓扑地图 Topological Map / Road Graph 3D栅格地图 ...
- [ZJOI2011]看电影(组合数学/打表+高精)
Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特 ...
- 【设计模式】Java设计模式精讲之原型模式
简单记录 - 慕课网 Java设计模式精讲 Debug方式+内存分析 & 设计模式之禅-秦小波 文章目录 1.原型模式的定义 原型-定义 原型-类型 2.原型模式的实现 原型模式的通用类图 原 ...
随机推荐
- git笔记(2)-常见命令的使用(详解待续)
1. 常用命令 (1)git --help 帮助命令,其他的类似 (2)git branch 查看分支及其他(创建分支,查看远程分支名称等) (3)git checkout 切换分支以及其他 (3)g ...
- LeetCode算法题-Ugly Number(Java实现-四种解法)
这是悦乐书的第199次更新,第208篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第64题(顺位题号是263).编写一个程序来检查给定的数字是否是一个丑陋的数字.丑陋的数 ...
- ECharts图表之柱状折线混合图
Echarts 官网主页 http://echarts.baidu.com/index.html Echarts 更多项目案例 http://echarts.baidu.com/echarts2/ ...
- Spring Web项目spring配置文件随服务器启动时自动加载
前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启 ...
- [matlab] 20.图与网络 matlab自带函数使用
matlab自带的biography(产生一个句柄) 可以用于画图 R=[1 1 2 4 1 2 3 3 5 7 3 4 5 6 7 8]; % 起始节点编号 C=[2 3 3 3 4 5 5 6 6 ...
- Android 6.0以后的版本报错:open failed: EACCES (Permission denied)
Android 6.0以后的版本报错:open failed: EACCES (Permission denied) 在开发项目中,遇见要进行文件操作,遇见Caused by: android.sys ...
- [SHOI2015]脑洞治疗仪
嘟嘟嘟 这题其实就是一个线段树维护最大连续和的水题. 别的操作不说,操作1只要二分找区间前\(k\)个0即可. 需要注意的是,因为操作1两区间可能有交,因此要先清空再二分查询-- 复杂度\(O(n l ...
- 【Vuex】mapGetters 辅助函数
mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性: import { mapGetters } from 'vuex' export default { // ...
- hadoop学习笔记肆--元数据管理机制
1.首先,认识几个名词 (1).NameNode中读.写.以及DataNode映射等信息叫做“元数据” ,NameNode元数据存放位置有.内存.fsimage.edits log三个位置. (2). ...
- ORA-28040: No matching authentication protocol
1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 告警日志中频繁出现Using depr ...