Esspe-peasee
Esspe-peasee is an alert ent game played by children throughout the land of acmania. The rules are simple:
A player simply quibs the Yorba at the kwonk. If the Yorba hurms the kwonk the player gets a foom. If the Yorba hurfs the kwonk the player gets a foob.
The objective is to get a twob with as few quibs as possible.
Every group of children has its own opinion regarding the value of a foom, the value of a foob, and the value of a twob. however, everyone agrees that a foob is worth more than a foom, and that a twob is worth more than a foob. you may assume that a foom and a foob can each be represented by a 32 bit integer, and a twob can be represented by a 64 bit integer.
Input
You will be given a number of game instances to solve. each instance is specified by 3 non-negative integers that represent the value of a foom, A foob and a twob, respectively. the final line contains three0'S and shoshould not be processed.
Output
For each instance your program shold print'AFooms andBFoobs for a twob!', On a line by itself as shown in the samples below, where the value''A"Fooms plus''B"Foobs add up to a twob, and the sum''A"And''B"Is as small as possible .''Fooms"And''Foobs"Shocould be appropriately pluralised, as shown in ''sample output" below.
If there is no such pair you shoshould print out the age-old chant :'Unquibable!'
Sample Input
1 6 157 9 227 9 320 9 182 5 90 0 0
Sample output
3 fooms and 2 foobs for a twob!Unquibable!2 fooms and 2 foobs for a twob!0 fooms and 2 foobs for a twob!2 fooms and 1 foob for a twob!
Extended Euclidean algorithms are no longer cumbersome. Orz
Summary by the way
If AX + by = C has a solution, that is, C % gcd (a, B) = 0, the following are all solutions:
If C = 1 & gcd (a, B) = 1
Special Solution (x0, y0)
General Solution (x0 + B * t, y0-a * t)
If C = _ c * gcd (A, B)
The old equation can be simplified to _ AX + _ BY = _ c gcd (_ A, _ B) Except for gcd (A, B) = 1 so it is converted to the above case _ AX + _ BY = 1
Obtain the special solution (x0 * _ c, y0 * _ C)
Therefore, the general solution is (x0 * _ c + _ B * t, y0 * _ c-_ A * t) (x0 * C/gcd (A, B) + B/gcd (a, B) * t, y0 * C/gcd (a, B)-A/gcd (a, B) * t)
This topic is worth noting and often used, that is, X, Y> 0 and minimum X + Y.
If you want x> 0, you can directly
X = (x * C % B + B) % B to find the smallest positive number x
Y = (c-A * X)/B to obtain the corresponding y
If y is <0, it is impossible to see X, Y at the same time> 0, because X is already the smallest positive number. If it is reduced, X is negative. If it is increased, y decreases, Y is negative
# Include <cstdio> # include <cstring> long a, B, c, d, X, Y; long exgcd (long a, long B, long long & X, long & Y) {If (B = 0) {x = 1; y = 0; return a;} long ret = exgcd (B, A % B, x, y); long ty = y; y = x-A/B * Y; X = ty; return ret;} int main () {While (scanf ("% LLD", & A, & B, & C )! = EOF & (A | B | C) {long d = exgcd (a, B, x, y); If (C % d! = 0) printf ("unquibable! \ N "); else {A = A/D; B = B/d; C = C/d; X = (X % B) * (C % B) % B) + B) % B; // X is just greater than 0, that is, the first one of X is smaller than 0. If y is smaller than 0, no solution is available. Y = (c-A * X) /B; If (Y <0) {printf ("unquibable! \ N "); continue;} If (x = 1) {printf (" 1 foom and "); If (y = 1) printf ("1 foob for a twob! \ N "); else printf (" % LLD foobs for a twob! \ N ", Y);} else {printf (" % LLD fooms and ", x); If (y = 1) printf (" 1 foob for a twob! \ N "); else printf (" % LLD foobs for a twob! \ N ", Y) ;}}return 0 ;}