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.原型模式的实现 原型模式的通用类图 原 ...
随机推荐
- Powershell测试端口状态
function Test-Port { Param([string]$ComputerName,$port = 5985,$timeout = 1000) try { $tcpclient = Ne ...
- sql server 的Maintenance Plans(维护计划)详解
下面说下我遇到的场景,就是我通过数据库自身的维护计划建立了数据库收缩自动计划,却发现数据库并没有实际性收缩. 前奏自动化配置流程 数据库--管理---维护计划--双击(维护计划向导)--下一步--名称 ...
- c/c++ 数组和指针
c/c++ 数组和指针 知识点 1,数组就是指针,对应代码里的test1 2,用auto声明,得到的是指针,对应代码里的test2 3,用decltype声明,得到的不是指针 ,对应代码里的test3 ...
- fedora 28/29 配置 C++ 环境
最近 使用C++ 开发 更换机器的时候,还要重新配置一下 gnu 工具链.于是简单进行了安装了一下: yum install gcc yum install gcc-c++ yum install g ...
- 注入攻击(SQL注入防御)
正确的防御SQL注入 sql注入的防御不是简单只做一些用户输入的escape处理,这样是不够的,只是提高了攻击者的门槛而已,还是不够安全. 例如 mysql_real_escape_string()函 ...
- Linux学习之路(一)
导语: 早前为了方便日常开发,建立跟生产环境类型的环境的时候考虑使用docker作为模拟生产环境,结果没想到给自己的学习挖了一个大坑.其他关于docker容器技术的坑先不在这里赘述,有时间的话在其他文 ...
- 日志收集(ElasticSearch)串联查询 MDC
之前写过将应用程序或服务程序产生的日志直接写入搜索引擎的博客 其中基本过程就是 app->redis->logstash->elasticsearch 整个链路过程 本来想将re ...
- 【Teradata】块压缩(ferret工具)
多值压缩(MVC) Enhanced Multi-Value Compression (MVC) or Value-List Compression• Compress VARCHAR, VARBYT ...
- 经常在比特币中看到的merkle树是什么?
区块基础-merkle树 Merkle tree中文叫做梅克尔树,这当然不是一棵真正的植物树,merkle tree是计算机数据结构中的一种树,是由计算机科学家 Ralph Merkle 提出的, ...
- Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口
(原) 这个接口很简单,里面只有一个抽象方法,没有default和静态方法. /* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. ...