题解:CF305B Continued Fractions含代码

· · 题解

思路

没有思路,直接无脑暴力约分即可

代码

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int N=100;
long long a[100],n,p,q,mg,x,y,num;
const unsigned long long mxll=1000000000000000000;
int main()
{
    cin>>p>>q>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    mg=__gcd(p,q);
    x=a[n];
    y=1;
    p/=mg,q/=mg;
    for(int i=n;i>=2;i-=1)
    {
        y=x*a[i-1]+y;
        swap(x,y);
        //cout<<x<<endl<<y<<endl;
        if(x>mxll||y>mxll) 
        {
            cout<<"NO";
            return 0;
        }
    }
    if(x==p&&y==q)
    {
        cout<<"YES";
    }else{
        cout<<"NO";
    }
    return 0;
}