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

http://codeforces.com/problemset/problem/305/B

题意:就是判断 p / q 等不等于那条式子算出来的值。

思路:一开始看到 1e18 的数据想了好久还是不会,暴力了一发显然错了。后来倒着想,就是 p / q(整除)的值一定要大于等于 a[i],否则就不行,每次判断完之后更新 p / q 即可,注意要判分母是否为0,因为这个RE了一次。思维僵化很严重,如果活跃一点估计很快就想出来了。吸取教训。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <string>
 7 #include <iostream>
 8 #include <stack>
 9 #include <map>
10 #include <queue>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 #define N 100010
15 #define INF 0x3f3f3f3f
16 #define MOD 1000000007
17 LL a[10005];
18 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
19 
20 int main()
21 {
22     LL p, q; int flag = 1;
23     cin >> p >> q;
24     int n; cin >> n;
25     for(int i = 1; i <= n; i++) cin >> a[i];
26     LL yue = gcd(p, q); p /= yue; q /= yue;
27     for(int i = 1; i <= n; i++) {
28         if(q == 0) { flag = 0; break; }
29         LL num = p / q;
30         if(num < a[i]) { flag = 0; break; }
31         p -= a[i] * q;
32         swap(p, q);
33         yue = gcd(p, q); p /= yue; q /= yue;
34     }
35     swap(p, q);
36     if(p == 0 && flag) puts("YES");
37     else puts("NO");
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/fightfordream/p/6298516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值