Problem E Codeforces Round #184 (Div. 2) B. Continued Fractions

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 integersa1, 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 cincout streams or the %I64dspecifier.

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 .

题目链接:http://codeforces.com/contest/305/problem/B

感想:当时太激动了  感觉方法正确 写出来了 不过就是少加了个判断导致WA了 后面又一直去检查别的程序去了  

        然后就没花多少时间来检查这题了   这说明做题一定要严谨  各种特殊情况都要考虑  不然就会无限的WA了

思路:从前往后推   模拟假分数化真分数   将得出来的整数部分与后面的ai比较(因为后面的形式为ai加一个分数形式  一般情况下后面的分数小于1 故ai即为后面的整数部分  当然当an为1时进行到最后第二项时后面的分数也可以为1喔) 若相同则 将真分数化假分数(倒过来) 右边的式子也倒过来  于是又变成ai+后面的分数形式了  这便可以循环执行了

ps:这题一般会有两种错误的思路  

       第一种:直接用double将右试从后往前算  算出来再与p/q比较  这个貌似有点天真  因为double的精度没那么高  当1/10^18时早就默认为0了

       第二种:将右试从后往前算  模拟通分约分  (这个算法应该也可以    不过得用高精度 解法参见博博学长的博客 http://my.csdn.net/dr5459  直接用  __int  64 来通分肯定会上溢

下面贴我的代码:(可能有点乱  因为大致是比赛时写的代码)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

__int64 p,q,a[100],t,temp,flag;   
// a[]用来存右试的每一位  flag-旗帜变量 存测试数据是否满足条件
int n;

void solve()
{
    int i,j;
    flag=1;
    for(i=1; i<=n; i++)
    {
        if(q==0)          // 循环过程中会出现 q为0的情况 即p/q以分解完  
        {                 
            flag=0;       // 如果程序还在循环 说明后面还有ai 不满足条件  
            break;
        }
        t=p/q;            // 取出p/q的整数部分
        if(t!=a[i])       // 如果左边和右边整数部分不相等  则判断之后break (注意要考虑an=1的情况)
        {
            if(i==n-1)    // 如果是循环到最后第二步 则可能发生特殊情况
            {
                if(p*1.0/q==a[i]+1.0/a[i+1])  // 直接判断p/q和后面两项相不相等
                {
                    q=0;                 // 这里后面再解释
                    flag=1;
                }
                else   flag=0;
            }
            else flag=0;
            break;
        }
        else
        {
            p=p-t*q;             // 模拟假分数化真分数 真分数化假分数
            temp=p;
            p=q;
            q=temp;
        }
    }
    if(q!=0)  flag=0; //  比赛时就是少加了这行判断 WA了  ╮(╯▽╰)╭
}                     //  如果循环出来 则要么不满足条件 要么是a1到an都执行完了
// 第一种情况不用考虑 因为flag已经为0  第二种情况 如果此时p/q还没有完的话 那么也是不满足条件的
// 现在应该理解第32行为什么要加q=0了吧  防止把满足条件的变为不满足条件
int main()
{
    int i,j;
    while(~scanf("%I64d%I64d",&p,&q))
    {
        scanf("%d",&n);
        for(i=1; i<=n; i++)
        {
            scanf("%I64d",&a[i]);
        }
        solve();
        if(flag) printf("YES\n");
        else  printf("NO\n");
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值