benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2021_C / Fair Division

Problem

Answer published by the source. Consult the official source to check your work against its answer.

platform

atcoder

question content

## Problem Statement After sailing the Seven Seas and raiding many ships, Cap’n Red and his crew of fellow pirates are finally ready to divide their loot. According to ancient traditions, the crew stands in a circle ordered by a strict pirate hierarchy. Cap’n Red starts by taking a fraction f f of the loot and passing the remainder on to the next pirate. That pirate takes the same fraction f f of the loot left over by the previous pirate and passes the remainder on to the following pirate. Each pirate behaves in the same way, taking a fraction f f of what is left and passing on the rest. The last pirate in the hierarchy passes the remainder on to Cap’n Red, who starts another round of this “fair” division, and so on, indefinitely. Fortunately, pirates in the 21st century can use a computer to avoid this lengthy process and constant nitpicking when the fraction f f does not exactly divide the loot at some step. You have been captured by the pirates and asked to come up with a suitable fraction f f . As an incentive, Cap’n Red has promised to leave you alive if you succeed. The fraction f f needs to be a rational number strictly between 0 and 1. It is not necessary that f f exactly divides the loot remaining at any step of the round-robin process described above. However, the total loot that would be assigned to each pirate by carrying out this process infinitely needs to be an integer. ### Input The input consists of one line with two integers n n and m m , where: - n n (6 ≤ n n 106 10^6 ) is the number of pirates including Cap’n Red. - m m (1 ≤ m m 1018 10^{18} ) is the total value of their loot. ### Output Output one line with two positive integers p p and q q , where f=pq f = \frac{p}{q} as specified above. If there are multiple suitable fractions, choose one with the smallest q q . Among multiple suitable fractions with the same smallest q q , choose the one with the smallest p p . If there is no suitable fraction, output `impossible` instead and hope for mercy. ### Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

After sailing the Seven Seas and raiding many ships, Cap’n Red and his crew of fellow pirates are finally ready to divide their loot. According to ancient traditions, the crew stands in a circle ordered by a strict pirate hierarchy. 

Cap’n Red starts by taking a fraction f of the loot and passing the remainder on to the next pirate. That pirate takes the same fraction f of the loot left over by the previous pirate and passes the remainder on to the following pirate. Each pirate behaves in the same way, taking a fraction f of what is left and passing on the rest. The last pirate in the hierarchy passes the remainder on to Cap’n Red, who starts another round of this “fair” division, and so on, indefinitely.

Fortunately, pirates in the 21st century can use a computer to avoid this lengthy process and constant nitpicking when the fraction f does not exactly divide the loot at some step. You have been captured by the pirates and asked to come up with a suitable fraction f. As an incentive, Cap’n Red has promised to leave you alive if you succeed.

The fraction f needs to be a rational number strictly between 0 and 1. It is not necessary that f exactly divides the loot remaining at any step of the round-robin process described above. However, the total loot that would be assigned to each pirate by carrying out this process infinitely needs to be an integer.

### Input

The input consists of one line with two integers n and m, where:

- n (6 ≤ n ≤ 10⁶) is the number of pirates including Cap’n Red.
- m (1 ≤ m ≤ 10¹⁸) is the total value of their loot.

### Output

Output one line with two positive integers p and q, where f=(p)/(q) as specified above. If there are multiple suitable fractions, choose one with the smallest q. Among multiple suitable fractions with the same smallest q, choose the one with the smallest p. If there is no suitable fraction, output `impossible` instead and hope for mercy.

### Sample Input 1
Original LaTeX notation
## Problem Statement

After sailing the Seven Seas and raiding many ships, Cap’n Red and his crew of fellow pirates are finally ready to divide their loot. According to ancient traditions, the crew stands in a circle ordered by a strict pirate hierarchy. 

Cap’n Red starts by taking a fraction \( f \) of the loot and passing the remainder on to the next pirate. That pirate takes the same fraction \( f \) of the loot left over by the previous pirate and passes the remainder on to the following pirate. Each pirate behaves in the same way, taking a fraction \( f \) of what is left and passing on the rest. The last pirate in the hierarchy passes the remainder on to Cap’n Red, who starts another round of this “fair” division, and so on, indefinitely.

Fortunately, pirates in the 21st century can use a computer to avoid this lengthy process and constant nitpicking when the fraction \( f \) does not exactly divide the loot at some step. You have been captured by the pirates and asked to come up with a suitable fraction \( f \). As an incentive, Cap’n Red has promised to leave you alive if you succeed.

The fraction \( f \) needs to be a rational number strictly between 0 and 1. It is not necessary that \( f \) exactly divides the loot remaining at any step of the round-robin process described above. However, the total loot that would be assigned to each pirate by carrying out this process infinitely needs to be an integer.

### Input

The input consists of one line with two integers \( n \) and \( m \), where:

- \( n \) (6 ≤ \( n \) ≤ \( 10^6 \)) is the number of pirates including Cap’n Red.
- \( m \) (1 ≤ \( m \) ≤ \( 10^{18} \)) is the total value of their loot.

### Output

Output one line with two positive integers \( p \) and \( q \), where \( f = \frac{p}{q} \) as specified above. If there are multiple suitable fractions, choose one with the smallest \( q \). Among multiple suitable fractions with the same smallest \( q \), choose the one with the smallest \( p \). If there is no suitable fraction, output `impossible` instead and hope for mercy.

### Sample Input 1

Code

8 51000

### Sample Output 1

Code

1 2

### Sample Input 2

Code

6 91000

### Sample Output 2

Code

2 3

### Sample Input 3

Code

10 1000000000000000000

### Sample Output 3

Code

impossible

question title

Fair Division

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import