B. Continued fractionstime limit per test
2 seconds
Memory limit per test
256 megabytes
Input
Standard Input
Output
Standard output
A continued fraction of heightNIs 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 heightN. Check if they are equal.
Input
The first line contains two space-separated IntegersP, Bytes,Q(1 digit ≤ DigitQLimit ≤ limitPLimit ≤ limit 1018 )-
The numerator and the denominator of the first fraction.
The second line contains integerN(1 digit ≤ DigitNLimit ≤ limit 90 )-
The height of the second fraction. The third line containsNSpace-separated IntegersA1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limit 1018 )-
The continued fraction.
Please, do not use the % LLD specifier to read or write 64-bit integers in bytes ++. It is preferred to use the CIN, cout streams
Or the % i64dspecifier.
Output
Print "yes" if these fractions are equal and "no" otherwise.
Sample test (s) 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.
Link: http://codeforces.com/contest/305/problem/ B
Feeling: I was so excited at the time. I felt that the method was correctly written, but I did not add a judgment. As a result, WA had been checking other programs.
Then it won't take much time to check this question. This shows that the question must be rigorous in various special situations. Otherwise, it will be infinitely wa.
Ideas: the integer part obtained from the previous and subsequent simulated false score-based real score is compared with the following AI (because the latter is in the form of AI plus a score, generally the latter is smaller than 1, so AI is for the next integer, of course, when an is 1, the score after the last second item can also be 1) if the score is the same, the formula on the right of the real score false score (inverted) is also reversed, and then it becomes the score form after Ai +. This can be executed cyclically.
PS: This question generally has two incorrect ideas.
First, directly use double to calculate the right trial from the back to the front and compare it with P/Q. This seems naive because the double precision is not so high. When 1/10 ^ 18, the default value is 0.
The second type: the Right trial from the back to calculate the simulation of the general score (this algorithm should also be able to use a high-precision solution, see the blog http://my.csdn.net/dr5459
If you use _ int 64 directly, the total score will surely overflow)
Paste my code below: (it may be a bit messy because it is roughly the code written during the competition)
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <string> # include <algorithm> # include <map> using namespace STD; __int64 p, q, a [100], T, temp, flag; // A [] is used to store each flag in the right test-whether the test data meets the condition int N; void solve () {int I, j; flag = 1; for (I = 1; I <= N; I ++) {If (q = 0) // when Q is 0, P/Q is decomposed into {flag = 0; // if the program is still following the loop description, there is also the AI that does not meet the condition break;} t = P/Q; // retrieve the integer part of P/Q if (T! = A [I]) // If the integer on the left is not the same as the integer on the right, then the break is judged (note the case of AN = 1) {if (I = N-1) // If the loop is to the last step, special cases may occur {If (p * 1.0/q = A [I] + 1.0/A [I + 1]). // directly determine that P/Q and the two following items are not equal {q = 0; // here we will explain flag = 1;} else flag = 0 ;} else flag = 0; break;} else {P = p-t * q; // simulate false score true score false score temp = P; P = Q; Q = temp ;}} if (Q! = 0) Flag = 0; // This line is skipped during the competition) else} // If the loop is made, either the conditions are not met or A1 to an are all executed. // The first case does not need to be considered because the flag is already 0. In the second case, if p/Q if it is not complete, then it does not meet the conditions. // now we should understand what to add q = 0 to the 32nd behavior to prevent the int main () {int I, j; while (~ Scanf ("% i64d % i64d", & P, & Q) {scanf ("% d", & N); for (I = 1; I <= N; I ++) {scanf ("% i64d", & A [I]);} solve (); If (FLAG) printf ("Yes \ n "); else printf ("NO \ n");} return 0 ;}