Problem Statement
Xenia and Yvona have a new toy: a collection of wooden cubes. Each cube has a positive integer side length. They have a large enough supply of cubes of all possible sizes.
Both girls have decided to use exactly N cubes, placed on top of each other, to build a tower that would have height exactly H.
Calculate and return the largest possible positive difference between the volumes of their two towers.
Definition
- Class:
- CubeTower
- Method:
- difference
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long difference(int H, int N)
- (be sure your method is public)
Notes
- No weird cube placements allowed. The bottom cube in the tower is placed on the ground (so that its bottom face is horizontal), and each of the following cubes is placed onto the previous one so that the top face of the previous one and the bottom face of the current one touch and overlap partially.
- Watch out for integer overflow, the correct return value will sometimes overflow a 32-bit integer variable.
- The smallest cube has side = 1. It is not allowed to use cubes with side = 0.
Constraints
- H will be between 1 and 10^6, inclusive.
- N will be between 1 and H, inclusive.
Examples
4
2
Returns: 12
We want a tower of height 4 using exactly 2 cubes. There are three possible towers: start with a 3x3x3 cube and place a 1x1x1 cube on top of it start with a 2x2x2 cube and place another 2x2x2 cube on top of it start with a 1x1x1 cube and place a 3x3x3 cube on top of it The tower of the second kind has volume 8+8 = 16, the other two types of tower have volume 27+1 = 28. If one of the girls builds a tower of the second kind and the other girl a different tower, the difference between their volumes will be 28 - 16 = 12.
17
16
Returns: 0
There are multiple different towers the girls may build but they all share the same total volume. (Each of those towers consists of fifteen 1x1x1 cubes and one 2x2x2 cube.)
5
3
Returns: 12
1000000
1
Returns: 0
1000000
1000000
Returns: 0
1000000
123456
Returns: 673476816437812224
1000000
200000
Returns: 512001919977600000
1000000
7
Returns: 979573836842326320
1000000
10
Returns: 989973000242999280
999999
2
Returns: 749994750010499994
2304
453
Returns: 6352122486
4431
8
Returns: 85226204406
502
188
Returns: 31252164
112601
24315
Returns: 688161350588256
3
3
Returns: 0
1069
108
Returns: 890172216
11206
62
Returns: 1383965793600
80
10
Returns: 352800
442527
116963
Returns: 34507470793305480
12
6
Returns: 300
1
1
Returns: 0
296847
1013
Returns: 25890964760732820
80495
218
Returns: 517345194873684
1365
268
Returns: 1323717684
180692
553
Returns: 5845599295119726
503
15
Returns: 116364192
225958
11548
Returns: 9856918898043360
4
1
Returns: 0
15686
616
Returns: 3423142020540
638
59
Returns: 195037170
2042
1071
Returns: 918323250
922034
712507
Returns: 9198694647299262
4446
43
Returns: 85368997716
606006
59704
Returns: 163042473792348084
2
1
Returns: 0
7
1
Returns: 0
62594
2549
Returns: 216497143188690
118028
9
Returns: 1623568792737450
14941
6778
Returns: 544137841182
11413
1837
Returns: 878391738420
5
1
Returns: 0
770967
229140
Returns: 159068553865446672
13
5
Returns: 636
317660
174
Returns: 32000994352543116
5
5
Returns: 0
468600
2546
Returns: 101230515094913280
13023
2417
Returns: 1193376733476
1232
2
Returns: 1397919600
166585
1210
Returns: 4522901676715470
1902
1
Returns: 0
342292
3
Returns: 35647506771898464
945022
15
Returns: 840179090820636462
810917
327172
Returns: 113201494583579844
100971
1919
Returns: 971857913814816
774665
3
Returns: 413223961827410292
826046
58
Returns: 563369906454464232
491577
127793
Returns: 48143134540435680
726412
81201
Returns: 268600803818007948
977767
178
Returns: 934235866820164080
657826
1
Returns: 0
341011
21
Returns: 39558759367737168
811705
586
Returns: 533646988199247390
49244
2
Returns: 89554198972212
268627
2835
Returns: 18777188296677084
582496
111
Returns: 197513829542767062
945884
2595
Returns: 839335565900470290
212230
17515
Returns: 7382524829643840
835134
818
Returns: 580754558725011816
442265
14
Returns: 86057308755405666
884564
13885
Returns: 660048279587883816
368938
368932
Returns: 300
580534
552897
Returns: 21111536064612
269933
153732
Returns: 1569064543221000
411478
402900
Returns: 631407828492
107433
107432
Returns: 0
988440
955075
Returns: 37146032438340
961968
960070
Returns: 6848162412
241222
241004
Returns: 10501932
463703
463701
Returns: 12
588195
584701
Returns: 42691487916
9
3
Returns: 264
1000000
100
Returns: 999603029402029800
1000000
2
Returns: 749997000003000000
1000000
434567
Returns: 180778075289638080
1000000
5000
Returns: 985077805077990000
999999
4435
Returns: 986753869600551360
10000
50
Returns: 984971912400
1000000
3
Returns: 888882888900222216
23
7
Returns: 4656
1000000
79283
Returns: 780512568656669214
1000000
37478
Returns: 891729938471193612
11
8
Returns: 42
1000000
9
Returns: 987630321179357520
1000000
123
Returns: 999567946428002364