Pencil Factory

Time limit1sMemory limit128 MB

Summary
Count how many of k pencils get painted and varnished when each machine skips every (n+1)th and (m+1)th pencil, grouped into four categories.
Level

Medium4 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

In a pencil factory, every unfinished pencil is processed in the same order: first it is painted by the painting machine, and immediately afterwards it is handed to the varnishing machine to be varnished. However, neither machine is properly tuned.

After the painting machine has painted nn pencils in a row, it leaves the very next pencil unpainted and then starts counting again. The varnishing machine behaves the same way: after varnishing mm pencils, it leaves the next pencil unvarnished and then continues.

Because of this, the factory ends up producing four kinds of pencils:

  • pencils that are both painted and varnished (fully processed);
  • pencils that are neither painted nor varnished;
  • pencils that are painted but not varnished;
  • pencils that are varnished but not painted.

Every pencil is unpainted and unvarnished before processing begins.

Given three natural numbers nn, mm, and kk (the number of unfinished pencils to be processed), write a program that computes the number of fully processed pencils and the number of pencils of each incomplete kind.

For example, if n=3n=3, m=5m=5, and k=17k=17, the processing can be illustrated by the table below (✓ means the operation was performed, ✗ means it was not).

1234567891011121314151617
Painting✓✓✓✗✓✓✓✗✓✓✓✗✓✓✓✗✓
Varnishing✓✓✓✓✓✗✓✓✓✓✓✗✓✓✓✓✓

As the table shows, only 12 of the 17 pencils are fully processed. One pencil (the 12th) is neither painted nor varnished, one pencil (the 6th) is painted but not varnished, and three pencils (the 4th, 8th, and 16th) are varnished but not painted.

Input

The first line contains three natural numbers nn, mm, and kk, separated by spaces.

It is guaranteed that 0<n<1060 < n < 10^6, 0<m<1060 < m < 10^6, and 0<k<1090 < k < 10^9.

Output

Print four integers on a single line, in this order:

  1. the number of pencils that are both painted and varnished;
  2. the number of pencils that are neither painted nor varnished;
  3. the number of pencils that are painted but not varnished;
  4. the number of pencils that are varnished but not painted.

Print them in the order above, with a single space between adjacent numbers.

Examples2

  1. Example 1

    Input
    3 5 17
    
    Expected output
    12 1 1 3
    
  2. Example 2

    Input
    999999 999999 999999999
    
    Expected output
    999999000 999 0 0