Problem Statement
John and Brus are training for a card game tournament. John is practicing his shuffling technique. John is using a deck of n cards, numbered 1 to n from top to bottom. This initial deck is called the main deck. There are two additional decks on the table, called the left and right decks. These two decks are initially empty. To shuffle the deck, John will repeat the following sequence of actions m times:
- Move one card from the top of the main deck to the top of the left deck, then one card from the top of the main deck to the top of the right deck, then one card from the top of the main deck to the top of the left deck, and so on, until the main deck is empty.
- While the left deck is not empty, move one card from the top of the left deck to the top of the main deck.
- While the right deck is not empty, move one card from the top of the right deck to the top of the main deck.
Definition
- Class:
- TheCardShufflingDivTwo
- Method:
- shuffle
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int shuffle(int n, int m)
- (be sure your method is public)
Constraints
- n and m will each be between 1 and 1,000,000, inclusive.
Examples
5
1
Returns: 2
After the shuffling the card order (from top to bottom) will be 2, 4, 1, 3, 5.
5
2
Returns: 4
And here the card order will be 4, 3, 2, 1, 5.
2
10
Returns: 1
17
9
Returns: 2
1
3
Returns: 1
5
7
Returns: 3
5
7
Returns: 3
48
1
Returns: 2
57
25
Returns: 14
69
26
Returns: 16
88
52
Returns: 78
91
73
Returns: 2
65
7
Returns: 63
45
37
Returns: 2
11
23
Returns: 8
99
29
Returns: 50
34
91
Returns: 23
34
27
Returns: 8
73
52
Returns: 55
5
69
Returns: 2
148
1
Returns: 2
357
125
Returns: 32
369
226
Returns: 187
388
852
Returns: 193
291
573
Returns: 182
165
7
Returns: 128
545
437
Returns: 32
611
523
Returns: 271
799
529
Returns: 189
434
291
Returns: 308
757148
851001
Returns: 300266
413357
971125
Returns: 19168
598369
567226
Returns: 594726
749388
890852
Returns: 318320
766291
239573
Returns: 432807
38165
597007
Returns: 22228
615545
51437
Returns: 115672
289611
341523
Returns: 101186
427799
215529
Returns: 90056
16434
544291
Returns: 1838
957148
951001
Returns: 167358
913357
971125
Returns: 279980
998369
967226
Returns: 927632
949388
990852
Returns: 118714
1000000
1000000
Returns: 605496
999999
999999
Returns: 399680
555555
777777
Returns: 347222
100000
100000
Returns: 1024
6
3
Returns: 1
1
1
Returns: 1
999997
999997
Returns: 295776
1000000
999999
Returns: 302748
10
5
Returns: 10
999999
880977
Returns: 188063
1000000
100000
Returns: 230887
997763
997763
Returns: 402227
1235
124312
Returns: 16
999201
1000000
Returns: 243322
23423
34534
Returns: 7115
123456
12345
Returns: 71582
987654
987654
Returns: 821029
999998
999998
Returns: 199840
999
998001
Returns: 512
30
10
Returns: 1
50000
12345
Returns: 25166
100000
10000
Returns: 45453
987891
987653
Returns: 681077
100000
1000000
Returns: 29503
999979
1000000
Returns: 194388
16
5
Returns: 15
243254
234454
Returns: 9859
2
1
Returns: 2
999999
453123
Returns: 36044
1
2
Returns: 1
999999
888888
Returns: 185914
1000000
1000
Returns: 230887
8
2
Returns: 4
98452
1212
Returns: 18090
12345
1234
Returns: 11524
100
1000000
Returns: 1
954125
125487
Returns: 785528
987654
654321
Returns: 801392
1000000
999198
Returns: 551031
10
4
Returns: 5
4
2
Returns: 4
50
50
Returns: 4
999999
1000000
Returns: 799360
8
4
Returns: 7
4
3
Returns: 3
1
979797
Returns: 1
999999
999998
Returns: 199840
999997
1000000
Returns: 366214
1
92
Returns: 1
3
2
Returns: 1
1234
1234
Returns: 1024
42
68
Returns: 11
987933
999977
Returns: 269558
1
10
Returns: 1
7
3
Returns: 1
12
8
Returns: 9