Problem Statement
You are given a large square matrix A. The matrix has dimensions n times n and it has a very special form derived from a sequence of integers. The exact procedure used to generate A is given below.
A square submatrix S is a square subarray of A that is contiguous in both dimensions. More formally, a square matrix S of size m*m (for some m between 1 and n, inclusive) is a square submatrix of A if we can find offsets x and y for which S[i][j] = A[x+i][y+j] for all i, j between 0 and m-1, inclusive. Note that a square submatrix cannot be empty.
Your task is to find a square submatrix of A with the largest sum of all its elements, and to return that sum.
The matrix A will be constructed in three steps:
- We'll use a pseudorandom generator to generate B: a sequence of n integers.
- We will apply some edits to the sequence B.
- We will use the edited sequence B to define the matrix A.
In order to generate B you are given the
for i in 0..n-1: B[i] = (s div 2^20) modulo q + o s0 = (s * 621) modulo 2^51 s1 = (s * 825) modulo 2^51 s2 = (s * 494) modulo 2^51 s3 = (s * 23) modulo 2^51 s = s3 s = (s * 2^10 + s2) modulo 2^51 s = (s * 2^10 + s1) modulo 2^51 s = (s * 2^10 + s0 + 11) modulo 2^51
In the pseudocode shown above, ^ denotes exponentiation and div denotes integer division (e.g., 49 div 10 is 4). The variables s0 through s3 are temporary variables. All calculations can be done in signed 64-bit integer variables without overflowing. Note that all elements of B will be between o and o+q-1, inclusive.
In the second step we'll apply several edits to the sequence B.
You are given data about the edits in two equally long
for i in 0..|x|-1: B[ x[i] ] = y[i]
Finally, matrix A is constructed as follows: for all i and j between 0 and n-1, inclusive, A[i][j] = B[i]+B[j].
Definition
- Class:
- MaxSquare
- Method:
- getMaxSum
- Parameters:
- int, long, int, int, int[], int[]
- Returns:
- long
- Method signature:
- long getMaxSum(int n, long s, int q, int o, int[] x, int[] y)
- (be sure your method is public)
Notes
- From the constraints of the problem it follows that B will always contain between 1 and 105 integers, inclusive, and that their absolute values won't exceed 108.
- The process used to produce the sequence B is only used to keep the input size small. The reference solution would work for any sequence B that matches the constraints mentioned in the previous Note.
Constraints
- n will be between 1 and 105, inclusive.
- s will be between 0 and 251-1, inclusive.
- q will be between 1 and 108+1, inclusive.
- o will be between -108 and 108-q+1, inclusive.
- x and y will have the same length.
- x will contain between 0 and 500 elements, inclusive.
- x will be sorted in strictly increasing order.
- Each element of x will be between 0 and n-1.
- Each element of y will be between -108 and 108, inclusive.
Examples
6
10000000014
20
-12
{1}
{3}
Returns: 28
The initial array B generated by the PRNG is {4, -3, -9, -7, 5, 2}. Next, we set B[1] to 3. Then, we generate A that looks as follows: 8 7 -5 -3 9 6 7 6 -6 -4 8 5 -5 -6 -18 -16 -4 -7 -3 -4 -16 -14 -2 -5 9 8 -4 -2 10 7 6 5 -7 -5 7 4 The square submatrix with the largest possible sum has dimension 2x2 and is located in the top-right corner of A. The sum of its elements is 9 + 6 + 8 + 5 = 28. There is no square submatrix (of any dimensions) with a larger sum, so 28 is the correct return value.
7
10000000029
20
-12
{}
{}
Returns: 12
B = {4, -1, -7, -5, 3, -12, -4}
10
42
40
-5
{}
{}
Returns: 2660
B = {-5, 0, 25, 4, 11, 29, 18, 20, 23, 8}
100
207650457737033
20
-10
{14, 21, 22, 26, 45, 64, 67, 70, 73}
{5, -4, 6, 4, -4, 4, 2, -10, -4}
Returns: 3384
100
227292310727029
20
-10
{27, 48, 68, 78}
{-2, -1, 2, 8}
Returns: 22352
100
247375630138163
20
-10
{11, 20, 24, 26, 28, 31, 78, 98}
{7, 2, -2, -8, -2, -7, -9, -6}
Returns: 5452
100000
157239544619141
20
-10
{}
{}
Returns: 314496
100000
251552102396085
20
-10
{820, 2013, 5814, 5843, 14783, 14942, 20423, 21262, 22298, 24375, 30303, 31739, 35334, 37789, 49907, 50899, 53993, 56458, 56700, 57748, 59534, 59915, 61695, 64051, 66402, 67422, 74135, 75015, 77733, 79921, 84972, 88520, 89575, 93185, 99895}
{7, 9, -2, 4, 0, -1, -8, -10, 0, 5, 3, -9, 1, 8, 2, -1, 8, 1, 9, -7, 1, -3, 6, 0, -1, -5, 5, 4, -4, 5, -1, -8, 8, -8, 2}
Returns: 248376
100000
161381371990431
20
-10
{2175, 3928, 6674, 8173, 8759, 13414, 14359, 17556, 18333, 28359, 30251, 31845, 33469, 34593, 35374, 36500, 37214, 41609, 43204, 46208, 46661, 46849, 54597, 55882, 57099, 63231, 65687, 65839, 69528, 70129, 70924, 71453, 73926, 74288, 74821, 79075, 80142, 80238, 82070, 85414, 89293, 94301, 94587}
{3, -9, -10, -5, -10, 1, 9, -4, -8, 2, -10, 9, -3, 6, 3, -9, 1, -3, -5, -3, 1, -4, -9, 1, -4, 5, 0, -3, -5, 3, 3, 5, -4, 0, 7, 7, 8, -5, -2, -3, -7, -6, 9}
Returns: 176012
100000
112018656491454
20
-10
{287, 2528, 9085, 12428, 15415, 17523, 19653, 19936, 21189, 21317, 22948, 22993, 25624, 26006, 26463, 32147, 32254, 33386, 37262, 37428, 38687, 40131, 43918, 44605, 45216, 46669, 49285, 51566, 53104, 53701, 57120, 58124, 60806, 63317, 67371, 68012, 68185, 71167, 71491, 74627, 74976, 78248, 78429, 78739, 78763, 78923, 79285, 80730, 80852, 81892, 83105, 83324, 83633, 84882, 94983}
{-10, 7, -1, 2, -2, -3, -3, 9, 9, -7, 3, -2, 3, -1, -2, -9, -2, 7, -2, -6, -7, -10, 9, 0, -10, -4, 0, 4, 5, -4, -9, 6, 9, -9, 8, 1, 7, 4, -4, -1, 9, 0, 3, 4, 0, -1, -4, -2, 0, -6, 2, 6, 6, -5, -3}
Returns: 202496
100000
227413373039597
20000
-13000
{329, 830, 2102, 2120, 3065, 7928, 14416, 17017, 18568, 24844, 29401, 30444, 35057, 35543, 35659, 36571, 37314, 38997, 41533, 48802, 49724, 51342, 51479, 52236, 52784, 55157, 56556, 58329, 61351, 62499, 62668, 64377, 66221, 66421, 69849, 74499, 77725, 78747, 79077, 81112, 83495, 83528, 86757, 94511, 95814, 96562}
{-8878, -1088, -6910, -7700, 5299, 4769, 2367, 5518, -8550, -712, -218, -6868, 2887, 3825, -9410, -2847, -7132, -12592, -10096, 1781, -1200, -7991, 1930, 2686, -7689, -5832, 6780, 4289, -8097, -4564, 880, 5392, 5872, -5895, -4747, 1326, 981, 3788, -8130, -7304, 2721, 3681, 5263, -5526, -4853, 2758}
Returns: 2636288
100000
21578757471626
20000
-13000
{2030, 2774, 5387, 5647, 6312, 6695, 7297, 8139, 8171, 9959, 10529, 10751, 11790, 13425, 16784, 16846, 22147, 25934, 26781, 27297, 27931, 28382, 39776, 40603, 43330, 46474, 48290, 51044, 52606, 52864, 58182, 66009, 66374, 67591, 75672, 77125, 78754, 84054, 86273, 87609, 97097, 97828, 98989, 99511}
{-10596, -8172, 3128, -5685, -12524, 3517, 6430, -5681, -10911, -6828, -9391, -12645, -6614, -2854, 4579, 6103, -2318, 3099, 1365, -127, -7842, 6914, 1730, -6363, -6394, -11970, -11594, -10170, -8163, -6593, -2554, 1348, -12023, 5960, 2636, -11097, 6015, -5052, -2739, 5710, 2092, -8502, 1040, -11150}
Returns: 3622102
100000
202373620062152
20000000
-13000000
{124, 4630, 6065, 6812, 7709, 9226, 9518, 10355, 10505, 11367, 12261, 12289, 13319, 15839, 16133, 16314, 16359, 17387, 18343, 20495, 22604, 23058, 26032, 27294, 27571, 28676, 30170, 31687, 36877, 36914, 40590, 40937, 41055, 41231, 44826, 48873, 69141, 71883, 73190, 74788, 74876, 80084, 80156, 80295, 82532, 84949, 86269, 89188, 94319, 95514, 96675, 98715}
{5002955, -1822464, -3543740, 5852400, -11031201, -3204208, -9646032, -3948493, 6082992, 1552720, -10108499, 5533902, -5605595, 3327480, -4016162, 6103191, 299653, -5998897, -8550786, -9255164, 6495704, -6336903, -11749964, 484001, -3300786, 1930504, 2103266, -2857871, -7403638, 3574766, -11516417, 6085153, -1711015, -8396771, -9102804, -5897042, 2384023, -7119731, 4682857, 4060137, 1849645, 5281090, -10379797, -3159572, 2699497, 6616407, -5242657, -8463483, 1074783, -3402335, 6747903, -9935556}
Returns: 2868037020
100000
193329227340635
20000000
-13000000
{680, 5955, 8709, 11373, 14651, 17113, 18728, 19212, 20531, 23847, 25349, 28785, 28834, 35337, 36003, 36407, 46065, 47801, 50643, 52668, 55251, 56287, 58227, 59944, 61332, 61699, 62223, 65421, 65932, 66283, 68070, 75872, 76038, 77339, 78293, 79391, 80392, 83770, 85203, 86565, 87881, 92060, 92135, 92370, 92917, 93078, 94816, 96212, 96959, 97574, 99590}
{-7641951, -4214267, 3089460, -1898607, 6178300, -2259106, -5550966, 6534430, 2181639, 3886266, -9269115, -1144395, 1959571, -3216509, -992961, -4186288, -517402, 4933189, -9408064, -9032968, 150096, -8384370, -2273155, -4068009, 3411444, -2117653, -1611503, -5133245, -4019610, -12398758, -5539885, 1880937, -6266115, -593293, -978578, -230590, -3932081, -6279378, -6108556, -5957858, 3884614, -9063632, 5845278, -4182360, -4591076, -8328, -9465164, 4209678, -12834618, 6377891, -9567687}
Returns: 2343058368
100000
229799892612954
100000000
0
{}
{}
Returns: 984147523220400000
100000
204552402392627
100000000
-70000000
{}
{}
Returns: 4822446746
100000
162246890887036
100000000
-70000000
{}
{}
Returns: 4621628130
100000
24005443345830
100000000
-100000000
{}
{}
Returns: -40
100000
140370163541290
100000000
-100000000
{}
{}
Returns: -950
10
172129821101668
10
-10
{}
{}
Returns: -2
100000
216021535202586
1000000
-300000
{}
{}
Returns: 4020144459553386
100000
270464707057257
1000000
-400000
{}
{}
Returns: 1998478391600000
100000
235232008726510
1000000
-500000
{}
{}
Returns: 5766369766512
100000
266029745725915
1000000
-600000
{}
{}
Returns: 354337920
100000
86434027276444
1000000
-700000
{}
{}
Returns: 58588640
100000
17626998986922
1000000
-800000
{}
{}
Returns: 12562746
100000
103289700159882
1000000
-900000
{}
{}
Returns: 3856690
500
52575514571859
1000
-600
{3, 45, 52, 54, 57, 59, 62, 64, 97, 116, 123, 154, 163, 282, 317, 320, 347, 400, 418, 433, 449, 458, 473}
{-350, -560, -1, -473, -245, -25, 47, 148, -386, 183, -591, 218, 10, 115, -461, 220, -291, -382, -213, -216, 159, -268, -35}
Returns: 28944
500
247508122856196
1000
-600
{29, 59, 84, 112, 150, 164, 170, 181, 190, 200, 267, 356, 362, 367, 370, 372, 381, 394, 436, 440, 485, 499}
{-519, -490, -163, -463, 307, -324, 285, -196, -434, -477, 191, 312, 65, 238, 0, -165, -542, 68, 46, -299, 361, 185}
Returns: 86504
500
238562838790632
1000
-600
{15, 26, 63, 75, 112, 136, 144, 147, 188, 231, 245, 257, 259, 268, 270, 290, 295, 296, 327, 328, 346, 470, 473, 482}
{-549, 390, 294, -403, 239, -179, -9, 335, -286, 67, -487, 241, -279, -289, 232, 9, -31, 37, 382, -94, 28, -381, 21, 58}
Returns: 42112
500
276839116715849
1000
-600
{4, 15, 83, 90, 111, 116, 209, 213, 241, 251, 306, 313, 341, 354, 369, 438, 449, 463, 478, 482}
{296, 316, -580, 157, -396, -329, -214, -446, -257, 220, -339, 99, 234, 42, -8, -204, 86, -441, -397, 13}
Returns: 40376
500
255600958712603
1000
-600
{3, 90, 92, 122, 196, 238, 290, 295, 358, 385, 431, 432, 434, 441}
{-195, 267, -315, 5, -243, -100, 318, -214, -437, -214, -545, -102, -404, -473}
Returns: 36288
500
250658999654974
1000
-600
{66, 102, 106, 109, 157, 199, 213, 226, 235, 256, 275, 276, 337, 343, 354, 377, 390, 466}
{379, 85, 255, -405, 367, -357, 202, -586, 384, 82, -556, 278, 110, 284, 233, -552, 139, 134}
Returns: 51304
500
5647797197709
1000
-600
{28, 32, 41, 61, 76, 133, 241, 253, 256, 294, 297, 339, 345, 351, 356, 383, 387, 412, 414, 419, 425, 444}
{-192, 107, 289, 103, -188, -382, -260, -26, -510, 227, -550, -452, -227, -566, -420, 46, -119, 229, -319, 282, 51, -53}
Returns: 59640
500
152670827689281
1000
-600
{6, 19, 31, 60, 139, 197, 250, 294, 335, 375, 390, 404, 415, 419, 464, 473, 491}
{-569, -236, -482, -150, -505, -60, -284, -361, -336, 88, -63, 375, -544, -561, 272, 323, 284}
Returns: 163800
10
42
40
-5
{ }
{ }
Returns: 2660
1
23
23
23
{0 }
{-2 }
Returns: -4
100000
42
100
-100
{50000 }
{5000000 }
Returns: 248909332320
1
0
100
-10000000
{ }
{ }
Returns: -20000000
100000
0
1
-1
{50000 }
{100000 }
Returns: 5000100000
100000
1
1
-1
{0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900, 7000, 7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900, 8000, 8100, 8200, 8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000, 9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000, 10100, 10200, 10300, 10400, 10500, 10600, 10700, 10800, 10900, 11000, 11100, 11200, 11300, 11400, 11500, 11600, 11700, 11800, 11900, 12000, 12100, 12200, 12300, 12400, 12500, 12600, 12700, 12800, 12900, 13000, 13100, 13200, 13300, 13400, 13500, 13600, 13700, 13800, 13900, 14000, 14100, 14200, 14300, 14400, 14500, 14600, 14700, 14800, 14900, 15000, 15100, 15200, 15300, 15400, 15500, 15600, 15700, 15800, 15900, 16000, 16100, 16200, 16300, 16400, 16500, 16600, 16700, 16800, 16900, 17000, 17100, 17200, 17300, 17400, 17500, 17600, 17700, 17800, 17900, 18000, 18100, 18200, 18300, 18400, 18500, 18600, 18700, 18800, 18900, 19000, 19100, 19200, 19300, 19400, 19500, 19600, 19700, 19800, 19900, 20000, 20100, 20200, 20300, 20400, 20500, 20600, 20700, 20800, 20900, 21000, 21100, 21200, 21300, 21400, 21500, 21600, 21700, 21800, 21900, 22000, 22100, 22200, 22300, 22400, 22500, 22600, 22700, 22800, 22900, 23000, 23100, 23200, 23300, 23400, 23500, 23600, 23700, 23800, 23900, 24000, 24100, 24200, 24300, 24400, 24500, 24600, 24700, 24800, 24900, 25000, 25100, 25200, 25300, 25400, 25500, 25600, 25700, 25800, 25900, 26000, 26100, 26200, 26300, 26400, 26500, 26600, 26700, 26800, 26900, 27000, 27100, 27200, 27300, 27400, 27500, 27600, 27700, 27800, 27900, 28000, 28100, 28200, 28300, 28400, 28500, 28600, 28700, 28800, 28900, 29000, 29100, 29200, 29300, 29400, 29500, 29600, 29700, 29800, 29900, 30000, 30100, 30200, 30300, 30400, 30500, 30600, 30700, 30800, 30900, 31000, 31100, 31200, 31300, 31400, 31500, 31600, 31700, 31800, 31900, 32000, 32100, 32200, 32300, 32400, 32500, 32600, 32700, 32800, 32900, 33000, 33100, 33200, 33300, 33400, 33500, 33600, 33700, 33800, 33900, 34000, 34100, 34200, 34300, 34400, 34500, 34600, 34700, 34800, 34900, 35000, 35100, 35200, 35300, 35400, 35500, 35600, 35700, 35800, 35900, 36000, 36100, 36200, 36300, 36400, 36500, 36600, 36700, 36800, 36900, 37000, 37100, 37200, 37300, 37400, 37500, 37600, 37700, 37800, 37900, 38000, 38100, 38200, 38300, 38400, 38500, 38600, 38700, 38800, 38900, 39000, 39100, 39200, 39300, 39400, 39500, 39600, 39700, 39800, 39900, 40000, 40100, 40200, 40300, 40400, 40500, 40600, 40700, 40800, 40900, 41000, 41100, 41200, 41300, 41400, 41500, 41600, 41700, 41800, 41900, 42000, 42100, 42200, 42300, 42400, 42500, 42600, 42700, 42800, 42900, 43000, 43100, 43200, 43300, 43400, 43500, 43600, 43700, 43800, 43900, 44000, 44100, 44200, 44300, 44400, 44500, 44600, 44700, 44800, 44900, 45000, 45100, 45200, 45300, 45400, 45500, 45600, 45700, 45800, 45900, 46000, 46100, 46200, 46300, 46400, 46500, 46600, 46700, 46800, 46900, 47000, 47100, 47200, 47300, 47400, 47500, 47600, 47700, 47800, 47900, 48000, 48100, 48200, 48300, 48400, 48500, 48600, 48700, 48800, 48900, 49000, 49100, 49200, 49300, 49400, 49500, 49600, 49700, 49800, 49900 }
{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }
Returns: 59781398
100000
10000000014
20
-120
{1 }
{3 }
Returns: 6