#P1495. Pig Farm Puzzle
Pig Farm Puzzle
Pig Farm Puzzle
After Cao Chong famously solved the elephant problem, his father Cao Cao decided to let him manage a pig farm. However, Cao Chong was not very enthusiastic and intentionally did a sloppy job. When Cao Cao wanted to know the number of sows, Cao Chong decided to play a little trick on him.
For example, suppose there are 16 sows. If they are arranged into 3 pig pens, there will be 1 pig left out. If arranged into 5 pig pens, again 1 pig remains without a pen, and if arranged into 7 pig pens, 2 pigs are left without a pen.
Your task is to determine the exact number of sows given the following system of congruences:
$$ \begin{aligned} x &\equiv r_1 \pmod{a}\\ x &\equiv r_2 \pmod{b}\\ x &\equiv r_3 \pmod{c} \end{aligned} $$
In the sample above, the parameters are: a=3, b=5, c=7 and r1=1, r2=1, r3=2, and the answer is 16.
inputFormat
The input consists of two lines:
- The first line contains three positive integers
a
,b
, andc
which represent the number of pig pens available in each scenario. - The second line contains three non-negative integers
r1
,r2
, andr3
which represent the number of pigs left without a pen when usinga
,b
, andc
pens respectively.
outputFormat
Output the smallest positive integer x
that satisfies the following system of congruences:
$$ x \equiv r_1 \pmod{a}, \quad x \equiv r_2 \pmod{b}, \quad x \equiv r_3 \pmod{c} $$
sample
3 5 7
1 1 2
16