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

Input
9 4
2
2 4
Output
YES
Input
9 4
3
2 3 1
Output
YES
Input
9 4
3
1 2 4
Output
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+高精 / 数学)的更多相关文章

  1. CF 305B——Continued Fractions——————【数学技巧】

    B. Continued Fractions time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. java高级精讲之高并发抢红包~揭开Redis分布式集群与Lua神秘面纱

    java高级精讲之高并发抢红包~揭开Redis分布式集群与Lua神秘面纱 redis数据库 Redis企业集群高级应用精品教程[图灵学院] Redis权威指南 利用redis + lua解决抢红包高并 ...

  3. jzoj6005. 【PKUWC2019模拟2019.1.17】数学 (生成函数+FFT+抽代+高精)

    题面 题解 幸好咱不是在晚上做的否则咱就不用睡觉了--都什么年代了居然还会出高精的题-- 先考虑如果暴力怎么做,令\(G(x)\)为\(F(n,k)\)的生成函数,那么不难发现\[G^R(x)=\pr ...

  4. bzoj 3287: Mato的刷屏计划 高精水题 && bzoj AC150

    3287: Mato的刷屏计划 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 43[Submit][Status] Desc ...

  5. BZOJ5300 [Cqoi2018]九连环 【dp + 高精】

    题目链接 BZOJ5300 题解 这题真的是很丧病,,卡高精卡到哭 我们设\(f[i]\)表示卸掉前\(i\)个环需要的步数 那么 \[f[i] = 2*f[i - 2] + f[i - 1] + 1 ...

  6. 洛谷1601 A+B Problem(高精) 解题报告

    洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...

  7. zz高精地图和定位在自动驾驶的应用

    本次分享聚焦于高精地图在自动驾驶中的应用,主要分为以下两部分: 1. 高精地图 High Definition Map 拓扑地图 Topological Map / Road Graph 3D栅格地图 ...

  8. [ZJOI2011]看电影(组合数学/打表+高精)

    Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特 ...

  9. 【设计模式】Java设计模式精讲之原型模式

    简单记录 - 慕课网 Java设计模式精讲 Debug方式+内存分析 & 设计模式之禅-秦小波 文章目录 1.原型模式的定义 原型-定义 原型-类型 2.原型模式的实现 原型模式的通用类图 原 ...

随机推荐

  1. 自动化测试基础篇--Selenium select下拉框

    摘自https://www.cnblogs.com/sanzangTst/p/7681523.html 一.什么是下拉框 下拉框是多项选择项,选择其中一种,类似下面(以百度搜索设置为例) 源代码如下所 ...

  2. 2802:小游戏利用bfs来实现

    之前使用的是递归的方法来解决的问题,后来有点想用bfs(宽度优先搜索来尝试一下的想法,在网上看到有人使用了dfs(深度优先搜索)更加坚定了自己的这种想法. 这个方法首先是以顶点的四组开始,加入那些没有 ...

  3. 【项目 · Wonderland】需求规格说明书 · 终版

    [项目 · Wonderland]需求规格说明书 · 终版 Part 0 · 简 要 目 录 Part 1 · 流 程 / 分 工 Part 2 · 需 求 规 格 说 明 书 Part 1 · 流 ...

  4. python3编写网络爬虫17-验证码识别

    一.验证码识别 1.图形验证码的识别 识别图形验证码需要 tesserocr 库 OCR技术识别(光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程.)例如 中国知网注册页面 ht ...

  5. IntelliJ IDEA LicenseServer激活及使用

    一.激活 IntelliJ IDEA下载地址:https://www.jetbrains.com/idea/download/#section=windows 原文地址:http://blog.csd ...

  6. ES5-ES6-ES7_class类

    传统创建对象模板的方式 JavaScript 语言中,生成实例对象的传统方法是通过构造函数 //JavaScript 语言中,生成实例对象的传统方法是通过构造函数 function Point(x, ...

  7. Django-rest-framework 接口实现 rest_framework 中有已经定义好的 工具类 mixins generics viewsets

    rest_framework.mixins 请求业务 的 5 种实现 ​ mixin(混合类):不能单独使用,和其它类搭配起来使用(利用了Python支持多继承) rest_framework.mix ...

  8. redis学习笔记(二)-五种数据类型

    string hash hget ks k hset ks k v hgetall ks hdel ks k del ks hmset  ks k v k v list set zset 通用命令 快 ...

  9. UltraISO制作Ubuntu14.04 64bit到U盘文件载入不完整

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zinss26914/article/details/37728251 前言 今天新买的Thinkpa ...

  10. OAuth2基本概念和运作流程

    OAuth(开放授权)是一个关于授权的开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用.目前的版本是2.0版,本文 ...