Under construction

Still being built. Things may change

Golf Gophers

Time limit20sMemory limit1024 MB

Summary
Find the gopher count G between 1 and M by choosing blade counts for 18 windmills each night and observing the downward blade over N nights.
Level

Medium7 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

Last year, a bunch of pesky gophers moved into our orchard. We tried changing our line of work by opening a miniature golf course, but the gophers have followed us here. Once again, we need to find out how many gophers there are. We cannot observe them directly, because they are secretive and nocturnal, while we like to sleep at night. We know only that there are between 1 and MM gophers, inclusive.

Our mini golf course is known for having a small electronic windmill on each of its 18 holes. The ii-th windmill has BiB_i blades, where 2≤Bi≤182 \le B_i \le 18. The blades are numbered from 0 to Bi−1B_i-1 clockwise. Each night, before going to sleep, we turn off the windmills and set each one so that blade 0 points downward. This matters because the windmills must charge properly for the next day. When we wake up, however, the windmills have been disturbed. The course is in a windless area, so the mischievous gophers must be responsible.

Every night, all of the gophers emerge one by one. Each gopher independently chooses one windmill uniformly at random and rotates it counterclockwise by one blade. For example, take a windmill with 3 blades and blade 0 pointing downward. The first gopher to interact with it turns it so that blade 1 points downward. The next gophers to interact with it make the downward blade 2, then 0, then 1, and so on.

We have devised a plan. We designed the windmills so that the number of blades is easy to change, which lets us adjust the difficulty. Each night, before going to sleep, we can choose the number of blades on each of the 18 windmills within the given limits. The windmills need not have the same number of blades, and we need not make the same choices every night. In the morning, we observe the number on the downward-pointing blade of each windmill. We have NN nights to figure out GG, the number of gophers. Can you help us?

Constraints

1≤T≤201 \le T \le 20.

Examples1

  1. Example 1

    Input
    1
    1 1
    
    Expected output
    1