CF 305B——Continued Fractions——————【数学技巧】
2 seconds
256 megabytes
standard input
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.
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.
Print "YES" if these fractions are equal and "NO" otherwise.
9 4
2
2 4
YES
9 4
3
2 3 1
YES
9 4
3
1 2 4
NO
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——————【数学技巧】的更多相关文章
- Continued Fractions CodeForces - 305B (java+高精 / 数学)
A continued fraction of height n is a fraction of form . You are given two rational numbers, one is ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- Codeforces 305B:Continued Fractions(思维+gcd)
http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...
- 丑数<数学技巧>
题意:丑数就是质因子只有2,3,5 ,7,的数,另外1也是丑数.求第n(1=<n<=5842)个丑数,n=0,结束. 思路:.3.5或者7的结果(1除外).那么,现在最主要的问题是如何排序 ...
- 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 ...
- nyoj--1170--最大的数(数学技巧)
最大的数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个 ...
- CF Amr and Pins (数学)
Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- hdu 5675 ztr loves math(数学技巧)
Problem Description ztr loves research Math.One day,He thought about the "Lower Edition" o ...
- HDU更多的学校比赛9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学技巧】
困难,.,真,,,不是太困难 的问题是,有一个矩阵运算优化 您有权发言权N*K矩阵A给K*N矩阵B(1<=N<=1000 && 1=<K<=6).他们拿起了第一 ...
随机推荐
- .Net Core 项目部署IIS简单步骤
1.新建一个解决方案: 我习惯会把运行文件移至一级目录 然后清除CoreTest 文件夹里面的文件 2.在解决方案中新建一个项目 点击确认有,这里有几种选择类型,我一般选择空类型(这里需要注意一下,空 ...
- 端口以及服务常用cmd
netstat -ano 列出所有端口的情况 netstat -aon|findstr "49157" 查出特定端口的情况 ...
- 「POJ 2182」 Lost Cows
题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...
- vs2015+opencv3.3.1+ c++实现 静态背景下多运动目标提取,检测
静止背景下运动物体的提取,跟踪出运动轨迹 下载地址 https://download.csdn.net/download/li_haoren/10761361 1.两遍扫描法得到第n帧的连通域,分离出 ...
- Selenium辅助工具
下载Firefox39.0版本浏览器,安装firebug和FirePath.最新版的Firefox在扩展组件中无法找到firebug,可以使用旧的版本的Firefox浏览器. FirePath插件的使 ...
- JSON Path表达式
JSON Path 描述 $ 表示根元素 @ 表示当前节点 . 表示子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标识,如数组下标 [,] 支持迭代器中多选 [start:en ...
- 文件上传下载(C#,web,asp.net)
目的:在浏览器页面启动上传下载CS软件,实现文件的批量下载与上传. 技术路线: 开发上传下载客户端CS程序与注册程序,压缩放到服务器端指定位置: 开发服务器端程序用以接收上传请求,压缩放到服务器端: ...
- [Swift实际操作]八、实用进阶-(10)使用Swift创建一个二叉树BinaryTreeNode
1.二叉树的特点: (1).每个节点最多有两个子树(2).左子树和右子树是有顺序的,次序不能颠倒(3).即使某节点只有一个子树,也要区分左右子树 2.二叉查找树(Binary Search Tree) ...
- 5. 常见C语言字符串库函数的使用及实现
1. strncat 函数: [函数原型]#include <string.h> char *strncat( char *str1, const char *str2, size_t c ...
- Win7下C/C++跨平台开发工具IDE的安装之CodeBlocks
1. Win7下安装CodeBlocks: 下载带有mingw的CodeBlocks:http://www.codeblocks.org/downloads/26#windows 运行所下载程序: 点 ...