The main observation to solve this problem is that you can’t change the order of people in the queues.

So basically you have to find the permutation of all voters without changing the order of each queue.

The permutation of all voters is ( i=1NAi )!(~\sum_{i=1}^{N}A_i~)!. As the order of each queue won’t change, we can treat the voters of each queue as similar elements. Now, it’s just basic combinatorics (As if it was not before!).

So the answer is (i=1NAi)!i=1NAi!\frac{ (\sum_{i=1}^{N} A_i )!}{\prod_{i=1}^N A_i!}.

The complexity will be O(N)O(N) per test case after precalculating the factorials.

Statistics

55% Solution Ratio
NirjhorEarliest, Jun '22
AlfehsaniFastest, 0.1s
Sohel_RanaLightest, 9.0 MB
serotoninShortest, 724B
Toph uses cookies. By continuing you agree to our Cookie Policy.