Problem Statement
To celebrate the Lunar New Year that starts the Year of the Tiger, the problems in this set feature tigers.
Absurdistan has always been famous for having a population of oxen and tigers living peacefully together. However, recently there has been an issue of contention: how to celebrate the Lunar New Year? The tigers want huge fireworks while the oxen consider it a waste of money. As always, the final decision will be decided by a referendum: a nation-wide vote.
The tigers really want the fireworks, so they decided to help their issue by cunningly manipulating the voting system. Instead of simply counting all the votes, they will divide the country into several exactly equally large districts. Each district will take its own vote on the issue and then it will nominate one delegate (either a tiger or an ox) to represent the opinion that won in the district. Those delegates will then cast the votes that will determine the final outcome of the referendum.
And the tigers are not required to stop at one level of hierarchy. For the next step, each district can now be divided into sub-districts. The sub-districts must again have exactly the same size, all over the country, so that everything seems as fair as possible. Each sub-district will take a vote and nominate one sub-delegate for the district vote, and then each district will evaluate the votes of those sub-delegates and nominate one delegate for the country vote.
Obviously, the above process can be continued recursively arbitrarily many times. Again, we emphasize that on each level of the election hierarchy all sub*-districts across the whole country must have exactly the same size.
One final peculiarity of the voting system in Absurdistan is that a tiger is TV / OV times more powerful than an ox, and therefore a tiger's vote is worth TV and an ox's vote is worth OV.
As tigers are the ones who want change, they must have a strict majority in order for the motion to pass. (If in some vote the total weight of the tigers' votes is the same as the total weight of the votes of the oxen, an ox is nominated as the delegate.)
The total population of Absurdistan is N. Assume that tigers have perfect control over how to divide the country into districts - they get to choose the depth of the hierarchy, the district sizes, and they also decide which inhabitants belong into which districts. Calculate and return the minimal number of tigers in the population needed to get the fireworks.
Definition
- Class:
- Tigerrymander
- Method:
- minTigers
- Parameters:
- long, int, int
- Returns:
- long
- Method signature:
- long minTigers(long N, int TV, int OV)
- (be sure your method is public)
Constraints
- N will be between 1 and 5 * 10^12, inclusive.
- TV will be between 1 and 10, inclusive.
- OV will be between 1 and 10, inclusive.
Examples
4
1
1
Returns: 3
There are 4 animals in Absurdistan, and the votes of tigers and oxen count the same. The tigers could divide the country into four districts of size 1, but that's the same as not dividing it at all: each district would simply nominate its only member. The tigers could also divide the country into two districts of size 2. This isn't exactly helpful: whenever there are two votes, both voters must be tigers in order for the tigers' motion to win. And thus if there are two districts of size 2, the tigers will only get fireworks if all four animals are tigers. Perhaps surprisingly, the best strategy here is to have everyone vote in the same election. Then three tigers can already outvote one ox.
13
2
1
Returns: 5
Again, the optimal solution is that there are no districts. Four tigers are not yet enough to win against nine oxen, but five tigers already have a stronger total vote than eight oxen.
12
7
7
Returns: 6
If everyone voted together, at least seven tigers would be needed. However, six tigers can still win the referendum by splitting the country cleverly. Using T and O to represent tigers and oxen, one optimal solution is to form four districts: TTO, TTO, TTO and OOO. Three districts will nominate a tiger while only one will nominate an ox, so in the final vote the tigers win 3:1. (There is one other solution that also allows six tigers to win the election.)
28
3
1
Returns: 2
Yes, you read that correctly, two tigers who cleverly manipulate the system can win the whole election here.
21
1
10
Returns: 20
1
1
1
Returns: 1
1
6
2
Returns: 1
2
1
1
Returns: 2
2
7
9
Returns: 2
3
1
1
Returns: 2
3
8
10
Returns: 2
4
1
1
Returns: 3
4
6
5
Returns: 1
5
1
1
Returns: 3
5
9
7
Returns: 3
6
1
1
Returns: 4
6
7
10
Returns: 4
7
1
1
Returns: 4
7
1
7
Returns: 7
8
1
1
Returns: 5
8
1
2
Returns: 6
9
1
1
Returns: 4
9
1
5
Returns: 8
10
1
1
Returns: 6
10
8
9
Returns: 6
11
1
1
Returns: 6
11
1
6
Returns: 10
12
1
1
Returns: 6
12
10
7
Returns: 2
13
1
1
Returns: 7
13
4
4
Returns: 7
14
1
1
Returns: 8
14
9
9
Returns: 8
15
1
1
Returns: 6
15
6
8
Returns: 6
16
1
1
Returns: 9
16
4
1
Returns: 1
17
1
1
Returns: 9
17
9
6
Returns: 7
18
1
1
Returns: 8
18
6
10
Returns: 8
19
1
1
Returns: 10
19
6
9
Returns: 12
20
1
1
Returns: 9
20
10
4
Returns: 2
21
1
1
Returns: 8
21
4
1
Returns: 2
22
1
1
Returns: 12
22
3
6
Returns: 15
23
1
1
Returns: 12
23
2
6
Returns: 18
24
1
1
Returns: 10
24
10
7
Returns: 2
25
1
1
Returns: 9
25
9
9
Returns: 9
26
1
1
Returns: 14
26
5
2
Returns: 4
27
1
1
Returns: 8
27
4
6
Returns: 8
28
1
1
Returns: 12
28
4
8
Returns: 15
29
1
1
Returns: 15
29
2
7
Returns: 23
30
1
1
Returns: 12
30
10
6
Returns: 4
31
1
1
Returns: 16
31
5
4
Returns: 14
32
1
1
Returns: 15
32
9
5
Returns: 1
33
1
1
Returns: 12
33
6
9
Returns: 14
34
1
1
Returns: 18
34
10
1
Returns: 2
35
1
1
Returns: 12
35
8
5
Returns: 6
36
1
1
Returns: 12
36
10
7
Returns: 4
37
1
1
Returns: 19
37
5
9
Returns: 24
38
1
1
Returns: 20
38
4
10
Returns: 28
39
1
1
Returns: 14
39
2
4
Returns: 27
40
1
1
Returns: 15
40
7
8
Returns: 15
53662
7
8
Returns: 16356
589056
5
6
Returns: 39600
1022400
7
7
Returns: 32400
2124084
7
6
Returns: 163392
5831070
7
6
Returns: 307584
7765740
4
7
Returns: 568512
7867000
6
9
Returns: 1510720
8454624
9
4
Returns: 27099
8685936
2
3
Returns: 665100
9921808
1
1
Returns: 1502613
11288538
3
8
Returns: 2918475
11883416
7
7
Returns: 1916720
18367392
10
7
Returns: 68160
18422580
7
6
Returns: 217728
27970584
5
5
Returns: 3067000
28516704
8
7
Returns: 277246
34301120
9
6
Returns: 55134
82672896
7
9
Returns: 9082800
83771088
7
1
Returns: 218154
98722688
6
10
Returns: 16296228
100889728
6
9
Returns: 12788190
112185024
2
10
Returns: 61351332
156196608
5
10
Returns: 32947803
302819328
1
3
Returns: 81842475
569423232
7
10
Returns: 22109400
628172800
10
3
Returns: 11328
636146688
8
6
Returns: 177498
666575360
1
7
Returns: 414404991
846027264
3
2
Returns: 176970
1266701312
4
10
Returns: 214710183
1835039232
1
10
Returns: 1173788935
4453904384
3
10
Returns: 1837671862
4620512256
6
3
Returns: 1002716
6815465472
8
8
Returns: 259991250
8840294400
5
2
Returns: 14100
10676264960
4
5
Returns: 488723625
10720370688
1
5
Returns: 4273394125
12151474176
5
6
Returns: 809091750
14627438592
7
5
Returns: 12300
15828172800
9
6
Returns: 42210
16936218624
2
4
Returns: 932678955
26233569280
1
10
Returns: 18176929875
29374980096
10
7
Returns: 34560
40363643520
1
4
Returns: 11782638000
53731360768
10
9
Returns: 776725
63132647424
10
1
Returns: 434
65081737216
9
6
Returns: 794455
117766356992
4
9
Returns: 6121708245
123245756416
3
3
Returns: 5289136875
127814664192
4
2
Returns: 433400
135050463602
10
1
Returns: 57769980
148406665216
10
9
Returns: 255013
158156437553
1
9
Returns: 115529027328
234453073920
7
4
Returns: 66720
246253223936
5
10
Returns: 10965714228
366207155757
3
2
Returns: 11574663936
396240041543
3
8
Returns: 210898711861
439977938400
2
8
Returns: 52390800000
539929972207
1
1
Returns: 135287194050
610840084480
7
5
Returns: 121932
642507465600
1
1
Returns: 587865600
656476128534
1
8
Returns: 522109669483
863837650962
1
5
Returns: 330291561600
894702483567
6
1
Returns: 4733875575
898910720836
9
6
Returns: 35967593888
905976981479
6
5
Returns: 187191953492
917192429149
1
8
Returns: 724870344160
930246454160
1
1
Returns: 78493543128
950872948546
1
10
Returns: 715519308591
1009116466889
10
1
Returns: 8355730635
1042794676224
1
6
Returns: 286200486936
1053659234304
9
4
Returns: 42578
1076544828635
9
1
Returns: 21530896573
1076879831137
9
7
Returns: 98394272120
1093424809205
2
3
Returns: 190577154144
1351478673408
8
10
Returns: 15028875000
1466005953063
1
1
Returns: 122316555150
1479680568748
1
1
Returns: 554880213282
1480188493824
8
8
Returns: 14749218750
1606268664000
9
4
Returns: 138240
1617878913351
1
8
Returns: 1278330318846
1670519410560
5
1
Returns: 1728
1711025316000
2
3
Returns: 7096320000
1799020903680
1
1
Returns: 1306368000
1927522396800
7
8
Returns: 1556755200
2137076602101
7
1
Returns: 23749947
2248776129600
2
1
Returns: 1935360
2318302247853
1
2
Returns: 1030384127704
2330482714938
8
2
Returns: 7398357826
2336782634030
1
1
Returns: 94771826856
2545945382114
1
1
Returns: 324170308800
2654245887419
7
1
Returns: 5925611730
2658965241938
3
1
Returns: 24453251635
2786526943200
1
1
Returns: 1306368000
2808194427352
1
3
Returns: 1382161548075
2891283595200
7
1
Returns: 108
2891283595200
8
4
Returns: 2580480
2974451444279
1
1
Returns: 427773458100
2983402795373
10
1
Returns: 1083789956
3029052236172
2
1
Returns: 56378298900
3140019277565
3
9
Returns: 1884011566540
3168982531120
8
10
Returns: 194019338880
3212537328000
1
1
Returns: 1763596800
3212537328000
1
9
Returns: 1377496834560
3212537328000
10
1
Returns: 48
3212537328000
10
4
Returns: 80640
3292325996836
2
5
Returns: 1348746994530
3373164194400
1
1
Returns: 1567641600
3373164194400
5
7
Returns: 5080320000
3373164194400
8
4
Returns: 3870720
3448539676197
2
2
Returns: 159239927424
3457551757423
1
8
Returns: 2450844824064
3464273851136
1
1
Returns: 289978280100
3597760450556
2
4
Returns: 545713425180
3747348352499
1
7
Returns: 2869209671328
3748406830777
1
1
Returns: 269301545920
3914001609231
5
5
Returns: 187065038160
4040150908193
2
1
Returns: 157814625930
4062684692965
10
1
Returns: 73866994418
4220150196398
1
1
Returns: 2110075098200
4265237242999
2
1
Returns: 160650956880
4291433687432
1
1
Returns: 170558196480
4302525256987
8
6
Returns: 790629817040
4310879103756
10
1
Returns: 1209562039
4315272922693
9
8
Returns: 213101190888
4323154990605
1
3
Returns: 1475551126300
4463376323439
1
1
Returns: 204954592752
4470935674380
1
1
Returns: 101972535552
4497552259200
1
1
Returns: 2351462400
4497552259200
1
10
Returns: 2033637838848
4497552259200
2
5
Returns: 131736084480
4497552259200
9
6
Returns: 10886400
4500005363403
1
1
Returns: 209357575680
4614493549680
9
1
Returns: 1078520
4706141764326
1
5
Returns: 2742334719117
4726514093172
8
1
Returns: 541488480
4746052164079
1
1
Returns: 1186581665610
4818805992000
1
1
Returns: 1959552000
4930463781687
6
1
Returns: 37071156255
4958340418274
5
10
Returns: 2203721132530