B. Continued Fractions
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
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 .

题目大意:判断两个式子是不是相等。

解题思路:p=q*a+r(a表示商,r表示余数)。则可以每次判断p-q*ai(相当于ai后边的分式的结果)的值是否小于0,如果小于0,说明肯定是不等的;如果大于0,将q当做p,将(p-q*ai)当做q(p、q的转变,表示将后边的一串分式取倒数)重复上边的过程。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL a[100];
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
int main(){
LL p,q,tm;
int n,i,j,k;
while(scanf("%I64d%I64d",&p,&q)!=EOF){ scanf("%d",&n);
bool flag=0;
for(i=0;i<n;i++){
scanf("%I64d",&a[i]);
if(flag==1){
continue;
}
if(p/q<a[i]){
flag=1;
continue;
}
tm=q;
q=p-q*a[i];
if(q<=0&&i!=n-1){
flag=1;
continue;
}
p=tm;
tm=gcd(p,q);
p/=tm;
q/=tm;
}
if(q>0)
flag=1;
if(flag==1){
printf("NO\n");
}else{
printf("YES\n");
}
}
return 0;
}

  

CF 305B——Continued Fractions——————【数学技巧】的更多相关文章

  1. Continued Fractions CodeForces - 305B (java+高精 / 数学)

    A continued fraction of height n is a fraction of form . You are given two rational numbers, one is ...

  2. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  3. Codeforces 305B:Continued Fractions(思维+gcd)

    http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...

  4. 丑数<数学技巧>

    题意:丑数就是质因子只有2,3,5 ,7,的数,另外1也是丑数.求第n(1=<n<=5842)个丑数,n=0,结束. 思路:.3.5或者7的结果(1除外).那么,现在最主要的问题是如何排序 ...

  5. 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 ...

  6. nyoj--1170--最大的数(数学技巧)

    最大的数 时间限制:1000 ms  |           内存限制:65535 KB 难度:3 描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个 ...

  7. CF Amr and Pins (数学)

    Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. hdu 5675 ztr loves math(数学技巧)

    Problem Description ztr loves research Math.One day,He thought about the "Lower Edition" o ...

  9. HDU更多的学校比赛9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学技巧】

    困难,.,真,,,不是太困难 的问题是,有一个矩阵运算优化 您有权发言权N*K矩阵A给K*N矩阵B(1<=N<=1000 && 1=<K<=6).他们拿起了第一 ...

随机推荐

  1. SQL/T-SQL实例参考-2

    对多关联查询,查询多中的记录,但是返回一的结果集 子查询语法 --一对多关联查询,查询多中的记录,但是返回一的结果集 SELECT C.* FROM ( SELECT A.BasicID FROM [ ...

  2. EIP权限工作流平台-升级说明(2018-12-04)

    表单生成器,文本框新增验证(默认验证及正则表达式) 列表查询支持复杂查询,支持文本框,下拉框,时间查询

  3. ecliplse启动慢的优化方法(亲测有效)

    本人使用的ecliplse IDE 版本是: Version: Photon Release (4.8.0)Build id: 20180619-1200 我的电脑配置是 Ecliplse启动差不多一 ...

  4. 三、SpringBoot-application.properties配置文件和application.yml配置文件

    其实SpringBoot的配置文件有.properties和.yml两种形式,两种配置文件的效果类似,只不过是格式不同而已,孩儿们可以根据下面这几种张截图,通过对比端口号的配置,以及连接SQLServ ...

  5. mysql 按照姓氏排序

    1.如果是gbk存储 order by name asc 2.非GBK order by convert(name using gbk) asc

  6. Java 根据Date计算年龄

  7. 2018数学建模A题优秀论文:高温作业专用服装设计

    高温作业专用服装设计 摘 要 本文针对多层材料的高温作业服装的传热问题进行研究,综合考虑多种传热方式建立传热模型,并以此模型为基础解决了服装设计中各层材料最佳厚度的问题. 对于问题一,要求在热物性系数 ...

  8. Selenium3+python异常后截图(screenshot)

    前言 在执行用例过程中由于是无人值守的,用例运行报错的时候,我们希望能对当前屏幕截图,留下证据. 在写用例的时候,最后一步是断言,可以把截图的动作放在断言这里,那么如何在断言失败后截图呢? 一.截图方 ...

  9. Puppet全面详解

    1.  概述 puppet是一个开源的软件自动化配置和部署工具,它使用简单且功能强大,正得到了越来越多地关注,现在很多大型IT公司均在使用puppet对集群中的软件进行管理和部署,如google利用p ...

  10. Servlet记录

    [Servlet]java语言编写动态资源的开发技术,普通的java类 [转发与重定向的区别] 转发在服务器端完成的:重定向是在客户端完成的 转发的速度快:重定向速度慢 转发的是同一次请求:重定向是两 ...