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.原型模式的实现 原型模式的通用类图 原 ...
随机推荐
- Python学习手记
1.Python大小敏感.print写作PRINT或Print是不对.2.注释符是“#”,而非“//”.3.语句结尾不必须分号.4.转义符为“/”+转义字母.这点与刀莱特一致.5.单引号输入使用“/' ...
- 洗礼灵魂,修炼python(71)--爬虫篇—【转载】xpath/lxml模块,爬虫精髓讲解
Xpath,lxml模块用法 转载的原因和前面的一样,我写的没别人写的好,所以我也不浪费时间了,直接转载这位崔庆才大佬的 原帖链接:传送门 以下为转载内容: --------------------- ...
- 监控MySQL或Web服务是否正常
在工作中,我们往往利用脚本定时监控本地.远端数据库服务端或Web服务是否运行正常,例如:负载高.cup高.连接数满了等.... 方法一:根据端口 本地:netstat/ss/lsof ① nets ...
- mysql 数据备份与数据导入到出
一.数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如Windows. #2. 逻辑备份: 备份的是建表.建库.插入等操作所执行SQL语句,适用于中小型数 ...
- keepalived脑裂问题查找
在自己环境做keepalived+redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...
- docker 中运行 mysql
在宿主机Host的终端执行: 1. 搜索docker镜像仓库中提供的mysql docker search mysql 2.下载mysql镜像 docker pull mysql 3.改变目录权限,非 ...
- 11175-From D to E and Back(思维)
Problem UVA11175-From D to E and Back Accept: 164 Submit: 607Time Limit: 3000 mSec Problem Descript ...
- UVA140-Bandwidth(搜索剪枝)
Problem UVA140-Bandwidth Time Limit: 3000 mSec Problem Description Given a graph (V, E) where V is ...
- CPU, PSU, SPU - Oracle Critical Patch Update Terminology Update
It all started in January 2005 with Critical Patch Updates (CPU). Then Patch Set Updates (PSU) were ...
- Zookeeper系列一:Zookeeper介绍、Zookeeper安装配置、ZK Shell的使用
https://www.cnblogs.com/leeSmall/p/9563547.html 一.Zookeeper介绍 1. 介绍Zookeeper之前先来介绍一下分布式 1.1 分布式主要是下面 ...