• Continued Fractions CodeForces


    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

    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 .

    思路:

    可以用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");
            }
            
        }
    
    }
    View Code

    还有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
    ",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), '', 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=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    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,1,n)
        {
            dll(a[i]);
        }
        int flag=0;
        repd(i,1,n)
        {
            if(q==0||(p*1.0000000/q)<a[i])
            {
                flag=1;
                break;
            }else
            {
                p-=q*a[i];
                swap(p,q);
            }
        }
        if(flag==0&&q==0)
        {
            printf("YES
    ");
        }else
        {
            printf("NO
    ");
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    View Code

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    怎样才有资格被称为开源软件
    [翻译]开发Silverlight 2.0的自定义控件
    网上Silverlight项目收集
    Google 分析的基准化测试
    IIS 承载的WCF服务失败
    Lang.NET 2008 相关Session
    Silverlight 2.0 beta1 堆栈
    asp.net 性能调较
    SQL Server 2005 的nvarchar(max),varchar(max)来救火
    LINQPad
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10339938.html
Copyright © 2020-2023  润新知