Codeforces 305B:Continued Fractions(思维+gcd)
http://codeforces.com/problemset/problem/305/B
题意:就是判断 p / q 等不等于那条式子算出来的值。
思路:一开始看到 1e18 的数据想了好久还是不会,暴力了一发显然错了。后来倒着想,就是 p / q(整除)的值一定要大于等于 a[i],否则就不行,每次判断完之后更新 p / q 即可,注意要判分母是否为0,因为这个RE了一次。思维僵化很严重,如果活跃一点估计很快就想出来了。吸取教训。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 100010
#define INF 0x3f3f3f3f
#define MOD 1000000007
LL a[];
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } int main()
{
LL p, q; int flag = ;
cin >> p >> q;
int n; cin >> n;
for(int i = ; i <= n; i++) cin >> a[i];
LL yue = gcd(p, q); p /= yue; q /= yue;
for(int i = ; i <= n; i++) {
if(q == ) { flag = ; break; }
LL num = p / q;
if(num < a[i]) { flag = ; break; }
p -= a[i] * q;
swap(p, q);
yue = gcd(p, q); p /= yue; q /= yue;
}
swap(p, q);
if(p == && flag) puts("YES");
else puts("NO");
return ;
}
Codeforces 305B:Continued Fractions(思维+gcd)的更多相关文章
- CF 305B——Continued Fractions——————【数学技巧】
B. Continued Fractions time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- Continued Fractions CodeForces - 305B (java+高精 / 数学)
A continued fraction of height n is a fraction of form . You are given two rational numbers, one is ...
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
- 【CodeForces 803 C】Maximal GCD(GCD+思维)
You are given positive integer number n. You should create such strictly increasingsequence of k pos ...
- codeforces 1030D Vasya and Triangle【思维+gcd】
题目:戳这里 题意:选出三个点构成三角形,要求面积为n*m/k. 解题思路:因为三个点的坐标都是正整数,根据三角形面积公式(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2=n* ...
- 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 ...
随机推荐
- Bootstrap 图片形状
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport&q ...
- 【windows】常见的系统环境变量,如%appdata%表示什么意思
原文:[windows]常见的系统环境变量,如%appdata%表示什么意思 1.介绍 %appdata%就代表了C:Users\用户名\AppData\Roaming这个文件夹. “%”是系统变量的 ...
- php将秒转换为 分:秒 函数
php将秒转换为 分:秒 函数 /** * 将秒转换为 分:秒 * s int 秒数 */ function s_to_hs($s=0){ //计算分钟 //算法:将秒数除以60,然后下舍入,既得到分 ...
- Xcode自动注释插件: VVDocumenter使用和安装
开源插件: VVDocumenter 下载地址: https://github.com/onevcat/VVDocumenter-Xcode 使用效果: 使用方法: 在方法写///,效果同上图,下面有 ...
- adb help
Android Debug Bridge version 1.0.32Revision cc6ae925e394-android -a - directs adb to listen on all i ...
- 对OO的封装了有了新的理解——希望是普通函数来写总体流程来统管类似的业务,但却又涉及具体操作的函数,仍然可以达到目的
就是不厌其烦,把那个具体操作函数封装成虚函数,只需要返回它的结果就行.而总体流程根据这个结果继续进行处理,这样就能总体流程和虚函数两不误了.
- Android零基础入门第26节:layout_gravity和gravity大不同
原文:Android零基础入门第26节:layout_gravity和gravity大不同 上一期我们一起学习了LinearLayout线性布局的方向.填充模型和权重,本期来一起学习LinearLay ...
- 小试X64 inline HOOK,hook explorer.exe--->CreateProcessInternalW监视进程创建
原始函数是这样的 kernel32!CreateProcessInternalW: 00000000`7738e750 4c8bdc mov r11,rsp 00000000 ...
- Oracle数据库密码重置、导入导出库命令
重置办法如下:打开CMD命令提示符,然后输入下面命令进行重置: 输入sqlplus /nolog,回车 SQL> conn /as sysdba 已连接: SQL>alter user s ...
- Codility---BinaryGap
Task description A binary gap within a positive integer N is any maximal sequence of consecutive zer ...