Problem Statement
Alice and Bob are going to play a simple dice game.
In the game, each player has their own six-sided die. Each player rolls their die and they look at the numbers they rolled. Whoever has the bigger number wins. If both numbers are the same, the game ends in a tie (nobody wins).
Alice and Bob like to play the game with custom dice. These don't have numbers 1 to 6 on their sides. Instead, the players choose what numbers they put onto their dice. They can choose any integer between 0 and 1000, inclusive, for each face. They can also put the same number on multiple faces of their die.
The game is considered fair if each player has the same probability of winning it.
Alice has already chosen the numbers on her die. You are given these in the
Bob wants to choose the remaining numbers for his die so that his game with Alice will be fair. Can he do that? If yes, how many different options does he have? Count them and return their count. Remember that each of Bob's missing numbers must also be an integer between 0 and 1000, inclusive.
Definition
- Class:
- TwoFairDice
- Method:
- finish
- Parameters:
- int[], int[]
- Returns:
- long
- Method signature:
- long finish(int[] A, int[] B)
- (be sure your method is public)
Notes
- Treat the ways of adding the numbers to Bob's die as ordered sequences of integers. For example, adding {1, 2, 2} and adding {2, 2, 1} should be counted as two separate options.
- (Equivalently, imagine that each of the faces of Bob's die is painted in a different color, and two ways are only equal if both numbers and colors match.)
Constraints
- A will have exactly 6 elements.
- Each element of A will be between 0 and 1000, inclusive.
- B will have between 0 and 6 elements, inclusive.
- Each element of B will be between 0 and 1000, inclusive.
Examples
{1, 2, 3, 4, 5, 6}
{6, 2, 3, 1, 5}
Returns: 1
The only way to make these two dice fair is to add the number 4 onto Bob's last face, thus creating two dice with the exact same probability distribution.
{1, 2, 3, 4, 5, 6}
{7, 8, 9, 10}
Returns: 0
Regardless of what two numbers we add to Bob's die, he will be the favorite in the game. There is no way to make these two dice fair.
{1, 2, 3, 4, 5, 6}
{7, 8, 9}
Returns: 1
The only way to make these two dice fair is to add three zeros.
{500, 500, 500, 500, 500, 500}
{}
Returns: 312505625007500001
Some of the very many ways to create a fair die for Bob in this test case include {500, 500, 500, 500, 500, 500}, {500, 100, 500, 500, 987, 500}, {987, 100, 500, 500, 500, 500}, and {1, 2, 3, 999, 998, 997}.
{2, 4, 6, 8, 10, 12}
{1, 3, 5, 9, 11, 13}
Returns: 1
Both dice are already complete and they happen to be fair, so there's exactly one valid way of finishing Bob's die: do nothing :)
{10, 20, 30, 40, 50, 60}
{}
Returns: 25276563851453
{10, 10, 11, 12, 12, 12}
{11, 12, 12}
Returns: 3
The only way that works here is adding a 12 and two 10s, thus making the two dice essentially equivalent. This should be counted as three ways: we can add {10, 10, 12}, {10, 12, 10}, or {12, 10, 10}.
{10, 10, 11, 12, 12, 12}
{10, 10, 10, 12}
Returns: 1976
Here we cannot make the two dice have the same multiset of values: Bob's die already has more 10s than Alice. But we can still make the game fair: we have to add one 12 and one number greater than 12 (i.e., anything in the 13-1000 range).
{440, 566, 566, 440, 64, 566}
{64, 64, 64, 584, 351}
Returns: 0
{939, 939, 741, 559, 939, 9}
{939, 939, 939, 920}
Returns: 1116
{807, 780, 860, 244, 860, 860}
{936, 36, 957, 200, 746, 200}
Returns: 0
{426, 762, 762, 112, 791, 762}
{791, 112, 893, 619}
Returns: 112227
{388, 116, 642, 388, 861, 642}
{}
Returns: 79018277705221144
{523, 523, 832, 637, 523, 637}
{559, 821, 523, 821}
Returns: 0
{974, 974, 849, 974, 974, 322}
{441}
Returns: 170994113450
{401, 352, 401, 352, 110, 110}
{273, 401, 68, 401, 273}
Returns: 48
{580, 577, 10, 577, 711, 577}
{}
Returns: 12640974062307356
{309, 309, 309, 309, 309, 309}
{55, 900}
Returns: 273544742395
{423, 423, 423, 423, 423, 423}
{427, 362, 427, 423, 423}
Returns: 423
{800, 800, 800, 800, 800, 800}
{800, 294, 800}
Returns: 96000600
{471, 1000, 283, 653, 962, 653}
{470, 557, 962, 283}
Returns: 1371
{900, 900, 900, 900, 900, 900}
{}
Returns: 14580729002700001
{976, 414, 828, 312, 976, 312}
{835, 856, 414, 976}
Returns: 624
{214, 214, 214, 214, 215, 296}
{214}
Returns: 6195381007380
{28, 28, 0, 28, 0, 886}
{532, 628, 886}
Returns: 0
{515, 583, 583, 515, 583, 515}
{525}
Returns: 94001637165007
{638, 79, 638, 79, 638, 638}
{420, 638, 61, 420, 61}
Returns: 0
{466, 759, 759, 466, 644, 759}
{125, 104, 410}
Returns: 13997521
{442, 335, 517, 335, 442, 517}
{323, 76}
Returns: 173736930586
{528, 659, 659, 229, 659, 229}
{519, 83}
Returns: 62139773920
{625, 485, 625, 485, 7, 603}
{}
Returns: 112663620938105790
{864, 864, 864, 864, 864, 886}
{145, 864, 886, 145, 187, 81}
Returns: 0
{939, 321, 939, 92, 92, 656}
{787, 572, 830, 830}
Returns: 61912
{360, 816, 531, 360, 533, 435}
{}
Returns: 59521016031640441
{98, 127, 596, 791, 791, 791}
{127, 866, 447, 447, 98}
Returns: 209
{505, 145, 333, 505, 333, 145}
{333, 792}
Returns: 811089140
{923, 378, 923, 923, 923, 378}
{378, 207, 378}
Returns: 17787
{578, 741, 741, 578, 578, 741}
{502}
Returns: 89466784976360
{532, 375, 353, 353, 375, 532}
{}
Returns: 120064929967704036
{232, 209, 232, 232, 232, 209}
{232, 951, 432, 209}
Returns: 418
{340, 340, 340, 410, 410, 410}
{933, 410}
Returns: 933594036
{840, 840, 840, 842, 842, 840}
{}
Returns: 46759388722784375
{963, 952, 952, 963, 691, 691}
{963, 691}
Returns: 8166365245
{746, 474, 416, 746, 746, 737}
{416, 416}
Returns: 1624666296
{998, 692, 692, 692, 692, 831}
{228, 228, 590, 88, 831}
Returns: 0
{268, 656, 656, 268, 656, 268}
{655, 151, 604, 247}
Returns: 118336
{171, 171, 874, 171, 594, 891}
{891, 594, 594}
Returns: 373977
{948, 948, 948, 980, 980, 316}
{620, 948}
Returns: 2169856
{538, 538, 781, 781, 36, 538}
{401, 103}
Returns: 9081124072
{879, 879, 816, 422, 40, 40}
{331, 492, 734, 331}
Returns: 96832
{784, 784, 784, 540, 784, 784}
{}
Returns: 31737982363404486
{918, 918, 960, 960, 389, 918}
{960, 623, 389, 176, 176}
Returns: 0
{265, 265, 265, 265, 265, 265}
{}
Returns: 147788168947587001
{381, 664, 545, 381, 230, 381}
{545, 230, 230, 623, 693, 623}
Returns: 0
{301, 301, 301, 301, 301, 301}
{38, 212, 38}
Returns: 341532099
{108, 429, 429, 153, 108, 153}
{873, 908, 908}
Returns: 1259712
{93, 93, 93, 93, 93, 93}
{321, 321, 708, 708, 93, 93}
Returns: 0
{784, 271, 271, 784, 60, 809}
{468, 60, 468, 523}
Returns: 1024
{891, 891, 891, 845, 891, 845}
{845}
Returns: 1324431755
{619, 619, 903, 903, 885, 645}
{}
Returns: 29902527824654681
{711, 952, 952, 952, 475, 711}
{}
Returns: 5865097892561715
{626, 331, 626, 331, 626, 626}
{626, 331, 164, 298, 164}
Returns: 0
{542, 775, 542, 566, 566, 775}
{291}
Returns: 63669708105575
{463, 536, 536, 536, 544, 544}
{571}
Returns: 207266173338570
{760, 520, 872, 520, 520, 281}
{281, 820, 281, 281, 982}
Returns: 1
{331, 331, 331, 331, 331, 331}
{560, 331}
Returns: 879557032
{80, 80, 892, 80, 892, 80}
{}
Returns: 23185093298677475
{305, 305, 305, 305, 282, 282}
{282, 771, 638, 200, 771}
Returns: 0
{13, 14, 2, 2, 2, 13}
{10, 13, 12, 1, 14, 8}
Returns: 0
{1, 7, 11, 7, 13, 1}
{10, 13, 8, 14, 7, 6}
Returns: 0
{1, 8, 15, 1, 1, 9}
{14, 12, 11, 9}
Returns: 0
{1, 1, 15, 8, 8, 1}
{1}
Returns: 364335230
{4, 4, 4, 4, 4, 4}
{}
Returns: 1266129980641
{8, 1, 8, 1, 1, 0}
{0, 1, 15, 1, 6}
Returns: 1
{1, 7, 12, 1, 7, 7}
{1, 7, 7, 12}
Returns: 2
{10, 15, 10, 15, 2, 2}
{1, 10, 13, 15}
Returns: 3999
{9, 13, 0, 0, 13, 13}
{15, 0}
Returns: 795312
{15, 8, 1, 1, 15, 9}
{1, 9}
Returns: 692236
{9, 12, 9, 12, 8, 8}
{}
Returns: 9895596022621
{3, 5, 13, 5, 5, 3}
{10, 10, 13, 10, 13, 10}
Returns: 0
{2, 1, 5, 1, 5, 5}
{14, 12, 1, 15, 13}
Returns: 0
{2, 2, 2, 2, 2, 2}
{13, 3, 3, 13}
Returns: 0
{12, 10, 12, 12, 10, 12}
{}
Returns: 19308614544775
{2, 7, 7, 8, 7, 2}
{15, 7, 2, 7, 2}
Returns: 0
{3, 3, 3, 3, 3, 7}
{4, 2, 5, 4, 7, 3}
Returns: 0
{2, 11, 1, 10, 6, 3}
{13}
Returns: 10972025
{2, 9, 9, 2, 2, 9}
{8}
Returns: 777235956
{11, 9, 13, 1, 9, 13}
{8, 11, 1, 1, 11}
Returns: 0
{2, 2, 6, 6, 2, 5}
{5, 9, 6, 7}
Returns: 0
{10, 10, 10, 12, 10, 10}
{13, 13, 1, 1, 13, 13}
Returns: 0
{12, 12, 12, 12, 12, 12}
{6, 6}
Returns: 46298509920
{1, 14, 13, 13, 1, 13}
{}
Returns: 107428155265
{6, 6, 6, 6, 6, 6}
{7, 4, 4, 7}
Returns: 11929
{12, 12, 12, 14, 14, 2}
{1}
Returns: 42050031030
{9, 15, 15, 15, 15, 15}
{15, 13}
Returns: 58225320
{12, 1, 12, 12, 12, 8}
{}
Returns: 62376600680
{11, 11, 11, 11, 5, 1}
{13}
Returns: 22441090
{7, 9, 9, 9, 9, 7}
{14, 7, 7, 4, 9}
Returns: 991