E - Continued Fractions
A continued fraction of height n is a fraction of form
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.
Example
Input
9 422 4
Output
YES
Input
9 432 3 1
Output
YES
Input
9 431 2 4
Output
NO
Note
In the first sample
In the second sample
In the third sample
题意:
计算p/q根据公式是否可以和下面的相等
思路:减法;p/q--下面第一个数然后在倒过来继续减,到为0为止(小与0没意义了)要判断等于0是不是把下面的数用完。没用完在继续减肯定会小0,所以是NO。这题后面的数据比较大,1818还是18位,所以可能会有巧合输出YES(有数据就是这个)。这时候就要判n/m<t的关系,(n,m大了可能会让n=tm...好神奇)
给一下比较坑的数据
Test: #40, time: 30 ms., memory: 1848 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
262882295792523313 105000000000078855
1
105000000000078855
Output
NO
Answer
NO
Checker Log
ok "NO"
Test: #44, time: 0 ms., memory: 1844 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
985625905209512860 565433601688714177
10
6423 24947 27507 13031 16414 29169 901 32592 18763 1656
Output
NO
Answer
NO
Checker Log
ok "NO"
题解
#include<stdio.h>
int main()
{
long long n,m,k,t,temp,i,x,flag;
scanf("%lld%lld",&n,&m);
{
flag=0;
scanf("%lld",&k);
for(i=0;i<k;i++)
{
scanf("%lld",&t);
x=n;
if(n/m<t)
{
printf("NO\n");
return 0;
}
n=n-t*m;
if(n==0)
{
break;
}
temp=m;
m=n;
n=temp;
}
if(n==0&&i==k-1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}