跳到正文

题解

题解:CF305B Continued Fractions含代码

s_u_n_s_h_i_n_e约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年6月12日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟
题目
CF305B

版本

  1. v1当前

思路

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

代码

C++
#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;
}
原文