Connection Status:
Competition Arena > TradeForTriples
TCO 2019 Final · 2019-11-14 · by lg5293 · Dynamic Programming
Class Name: TradeForTriples
Return Type: int
Method Name: findExpectation
Arg Types: (int, int, int, int)
Problem Statement

Problem Statement

You are given four ints: n, k, a, and b.

You have a deck of cards. Each card has a suit and a number. There are n different suits and k different numbers. There is a single card for each distinct (suit, number) pair, for a total of n * k cards in your deck.

You would like to get at least three cards in your hand that have the same number. Initially, all the cards are in the deck, and thus there are zero cards in your hand. You then repeat the following process.

  • Draw a cards uniformly at random from the deck, and put them into your hand. (The deck will always have at least a cards at this point).
  • If your hand has at least 3 cards of the same number, you win and stop.
  • Otherwise, while your hand contains more than b cards, choose a card from your hand and return it to the deck.

Each execution of the above sequence of steps takes one second.

You would like to know the expected value of the number of seconds you need to finish this game, given that you try to minimize the time needed (by choosing which cards to return in the third step). It is guaranteed this value exists for every possible input that satisfies the constraints.

Let M = 998244353. It can be shown that the exact expected value is a rational number. Let P and Q be relatively prime nonnegative integers such that the expected value is P/Q. It can be shown that Q and M are relatively prime. Compute and return the value (P * Q^(-1)) modulo M, where Q^(-1) is the inverse element to Q modulo M.

Constraints

  • n will be between 3 and 50, inclusive.
  • k will be between 1 and 50, inclusive.
  • a will be at least 1.
  • b will be at least 1.
  • a+b will be between 3 and n*k, inclusive.
Examples
0)
3
1
1
2
Returns: 3

In this case, we would draw cards one by one until we have all cards in our hand.

1)
3
2
1
2
Returns: 798595489

We can split this into two cases: The first two cards we draw are the same (happens with probability 2/5). It is optimal to wait until we draw the third card, and this takes 4 additional steps in expecation. The first two cards we draw are different (happens with probability 3/5). After drawing our third card, we get back to the first case, so this takes 5 additional steps in expectation. Thus, the answer is 2 + (4 * 2/5 + 5 * 3/5) = 33/5.

2)
39
13
5
9
Returns: 873239918
3)
29
12
5
9
Returns: 691554814
4)
50
50
99
75
Returns: 923403294

Submissions are judged against all 45 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class TradeForTriples with a public method int findExpectation(int n, int k, int a, int b) · 45 test cases · 2 s / 256 MB per case

Submitting as anonymous