1

We have contest, that contains two parts.

Part 1. Knockout until we have an odd number of players. In each round players are paired up; each pair plays a game with the winning player advancing to the next round (no ties). This part continues as long as number of players is even. When we come to round with are an odd number of players we go to part 2.

  • Part 1 will be skipped, if starting quantity of players is odd
  • If Part 1 ends with 1 player then contest is considered over, we got a winner

Part 2. Round-robin. Each participant plays every other participant once. The player with the most points is the winner.

How many players must be invited to participate in the contest for got N games to be played?

Here can be multiple correct numbers of players (2?).

Example 1. We need a contest with 3 games. We can invite 3 players, than contest start with part 2 and 3 games will played. Or we can invite 4 players and part 1 will be made with 3 games too. 2 semi-finals, final and we got a winner.

Example 2. We need contest with 12 games. We must invite 12 players. Contest start with Part 1, where will be played $6 + 3$ games, than 3 more games will be played in Part 2. So the got $6 + 3 + 3 = 12$ games.


My thoughts.

We have 3 variants of contest.

  1. Part 1 only. Number of players must be power of 2, then will be played $N - 1$ games. It's can be easily checked, just add 1 to N and take Log2, if result is not fractional than we got an answer.

  2. Part 2 only. Number of players must be a "triangular number". It's a little bit harder. Such quantity of players will be play $N * (N +1) / 2$ amount games. Possible amount of players for N games: $\frac{\sqrt{8 \times N + 1} + 1}{2}$. Again if result is not fractional than we got an answer.

  3. Most difficult variant. We have both parts, and no idea about players count. What can we understand from all of this...

  • number of players in part 2 minimum is 3

  • number of players in part 2 maximum = 1/2 part 1 minimum

  • maximal count of games in part 2 = N - num of Part 1 games

    • For example, we need 946 games, then we invite 86 players, wich will play 43 games in Part 1 and 903 games in Part 2.
  • maximal count of games in part 1 = N - 3

    • For 768 games we need invite 768 players, wich will play 765 games in Part 1 and 3 games in Part 2.
  • number of games in part 1 always is odd

  • number of players in part 2 less that number of players in part 1 in "power of 2" times (2,4,8,16...)

My naive solution for variant #3.

Start from 3 and increase number of players in part 2 until count of games in part 2 less than N - part 2 players. Check all possible counts. It's work, but for N = 10^18 amount of iterations ~ 10^9, that looks not efficient.

Pradox69
  • 13
  • 3

1 Answers1

2

If the number of players is $2^a(2b+1)$ then the total number of games played is $(2^a-1)(2b+1) + (2b+1)b$, since we need to eliminate $(2^a-1)b$ players in the first part to get it down to $b$ players,and then in part $2$ we get $\frac{(2b+1)(2b+1-1)}{2}$ games.

Let $n$ be the desired number of games, we must thus solve $(2^{a}+b-1)(2b+1) = n$, I'm not sure if there is a very general way to know how many solutions exist, but one way to do it is to iterate through the odd divisors $2b+1$ of $n$ and for each one check if $n/(2b+1) - b+1$ is a power of $2$. Alternatively you can solve the quadratic polynomial in b that arises from fixing $2^a$ if you don't wish to implement pollard-rho or some other factorization algo, both should yield $\mathcal O(\log(n))$ solutions.

Asinomás
  • 107,565
  • a - count of games at Part 1 and b - count of games at Part 2? – Pradox69 Aug 31 '21 at 17:06
  • no, we are writing the number of participants in the form $2^a(2b+1)$, Every positive integer is uniquely expressible as the product of a power of $2$ and an odd integer. Writing it in this way seems to allow us to extract a nice formula for the total number of games. https://math.stackexchange.com/questions/486395/expression-of-an-integer-as-a-power-of-2-and-an-odd-number-chartrand-ex-5-4-2a – Asinomás Aug 31 '21 at 17:10
  • 1
    Thanks, iterating $2^a$ in polynomial equation is exactly what I looking for. – Pradox69 Sep 01 '21 at 07:11
  • My pleasure :) . – Asinomás Sep 01 '21 at 07:14