• 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/ 希望所写的文章对您有帮助。
  • 相关阅读:
    再谈:我对测试行业发展和自我价值诉求的思考
    如何写出让业务满意的性能测试报告
    面试突击17:HashMap除了死循环还有什么问题?
    Spring Boot Admin,贼好使!
    为什么HashMap会产生死循环?
    更快的Maven构建工具mvnd和Gradle哪个更快?
    交互软件Flinto Principle
    如何避免 加和浮点数的问题
    Oracle Linux 8.5 For windows
    三门问题
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10339938.html
Copyright © 2020-2023  润新知