Talk:UVa 10576 - Y2K Accounting Bug

Hey guys, we do not need to generate all solutions to discover the best surplus, The best surplus will occur when the months on deficits are placed in the months that most appear in each reports, please see this table:

/** Reports per months: R1: 1 2  3  4  5

R2: 2 3  4  5  6

R3: 3 4  5  6  7

R4: 4 5  6  7  8

R5: 5 6  7  8  9

R6: 6 7  8  9  10

R7: 7 8  9  10 11

R8: 8 9  10 11 12

For the input, surplus: 59, deficit: 237, we know that 4*surplus and 1 deficit is enough for making a report of 5 sequential months negative, so, if we place the deficit in the month that most appear from R1 to R5 (5th month), and from R6 to R8(10th month), we've got the best scenario:

R1: 0 0 0 0 1
 * Best for 1 deficit per report:

R2: 0 0 0 1 0

R3: 0 0 1 0 0

R4: 0 1 0 0 0

R5: 1 0 0 0 0

R6: 0 0 0 0 1

R7: 0 0 0 1 0

R8: 0 0 1 0 0  only 2 deficits!!! (5th and 10th)

And the others follow a similar pattern:

R1: 0 0 0 1 1
 * Best for 2 deficits per report:

R2: 0 0 1 1 0

R3: 0 1 1 0 0

R4: 1 1 0 0 0

R5: 1 0 0 0 1

R6: 0 0 0 1 1

R7: 0 0 1 1 0

R8: 0 1 1 0 0  only 4 deficits!!! (4th, 5th and 9th, 10th)

R1: 0 0 1 1 1
 * Best for 3 deficits per report:

R2: 0 1 1 1 0

R3: 1 1 1 0 0

R4: 1 1 0 0 1

R5: 1 0 0 1 1

R6: 0 0 1 1 1

R7: 0 1 1 1 0

R8: 1 1 1 0 0  only 6 deficits!!! (3th, 4th, 5th and 8th, 9th, 10th)

R1: 0 1 1 1 1
 * Best for 4 deficits per report:

R2: 1 1 1 1 0

R3: 1 1 1 0 1

R4: 1 1 0 1 1

R5: 1 0 1 1 1

R6: 0 1 1 1 1

R7: 1 1 1 1 1

R8: 1 1 1 1 0  only 9 deficits!!! (2th, 3th, 4th, 5th and 7th, 8th, 9th, 10th, 11th)

One simple set of masks is enough to calculate it:

static const size_t MASKS[] = { 0b000000000000, // Impossible to make a report negative 0b001000010000, // when there's 1 month on deficit per report 0b001100011000, // when there are 2 months on deficit per report 0b001110011100, // when there are 3 months on deficit per report 0b011111011110, // when there are 4 months on deficit per report 0b111111111111, // when there are 5 months on deficit per report, DEFICIT for sure };

to calculate the balance, just iterate the bits in the mask, if it's 1, you subtract the deficit, otherwise sum the surplus