Problem Statement
Fox Ciel is returning from her trip to New Fox Land.
She is now waiting for her flight in the terminal.
She still has some money left and she wants to spend all of it on souvenirs.
There is a rectangular grid of vending machines. The grid has n rows by m columns. Both rows and columns are numbered starting at zero. Each vending machine sells just one type of souvenirs. Different machines sell different souvenirs. Some vending machines may be broken. Broken machines don't sell anything.
The vending machine in row r, column c is described by the value price[r*m+c]. If this value is positive, it is the cost in dollars of buying a single souvenir in the machine. If the value is zero, the machine is broken.
Fox Ciel now wants you to answer multiple queries. For each valid i, query i has five parameters: xMin[i], xMax[i], yMin[i], yMax[i], and require[i]. The query you should answer looks as follows:
You are given the dimensions n and m, the int[] price, and the int[]s xMin, xMax, yMin, yMax, and require. Return a int[] with as many elements as there are queries. For each valid i, element i of the return value should be the answer to query i.
There is a rectangular grid of vending machines. The grid has n rows by m columns. Both rows and columns are numbered starting at zero. Each vending machine sells just one type of souvenirs. Different machines sell different souvenirs. Some vending machines may be broken. Broken machines don't sell anything.
The vending machine in row r, column c is described by the value price[r*m+c]. If this value is positive, it is the cost in dollars of buying a single souvenir in the machine. If the value is zero, the machine is broken.
Fox Ciel now wants you to answer multiple queries. For each valid i, query i has five parameters: xMin[i], xMax[i], yMin[i], yMax[i], and require[i]. The query you should answer looks as follows:
- Ciel has exactly require[i] dollars and she wants to spend all of them on souvenirs.
- Ciel only wants to use vending machines in some specific rectangular area. More precisely, she will only use machines in rows xMin[i] to xMax[i], inclusive, and columns yMin[i] to yMax[i], inclusive.
- In each vending machine she will buy at most one souvenir (she doesn't want to have duplicates).
- The answer to the query is the number of ways in which she can spend her money, modulo 1,000,000,009.
You are given the dimensions n and m, the int[] price, and the int[]s xMin, xMax, yMin, yMax, and require. Return a int[] with as many elements as there are queries. For each valid i, element i of the return value should be the answer to query i.
Definition
- Class:
- FoxAndSouvenir
- Method:
- waysToBuy
- Parameters:
- int, int, int[], int[], int[], int[], int[], int[]
- Returns:
- int[]
- Method signature:
- int[] waysToBuy(int n, int m, int[] price, int[] xMin, int[] xMax, int[] yMin, int[] yMax, int[] require)
- (be sure your method is public)
Constraints
- n, m will be between 1 and 50, inclusive.
- price will contain exactly n*m elements.
- Each number in price will be between 0 and 2,500, inclusive.
- The sum of all integers in price will be no more than 2,500.
- xMin will contain between 1 and 2,500 elements, inclusive.
- xMin, xMax, yMin, yMax and require will contain the same number of elements.
- Each element in xMin and xMax will be between 0 and (n-1), inclusive.
- Each element in yMin and yMax will be between 0 and (m-1), inclusive.
- For each i, xMin[i] will be less than or equal to to xMax[i].
- For each i, yMin[i] will be less than or equal to to yMax[i].
- Each element in require will be between 0 and 2,500, inclusive.
Examples
1
5
{5,0,5,7,7}{0}{0}{0}{4}{12}Returns: {4 }We have 4 vending machines, each have a souvenir with price 5, 5, 7, 5. We want a total price = 12, we must select 1 with price = 5 and another with price = 7. So there are 2*2 = 4 ways to get that.3
3
{1,2,3, 4,5,6, 7,8,9}{0,0,0,0,0,0,0,0,0,0}{2,2,2,2,2,2,2,2,2,2}{0,0,0,0,0,0,0,0,0,0}{2,2,2,2,2,2,2,2,2,2}{1,2,3,4,5,6,7,8,9,10}Returns: {1, 1, 2, 2, 3, 4, 5, 6, 8, 9 }For example, there are 3 ways to get 5: {1,4}, {2,3}, {5}.1
10
{1,0,2,1,0,3,1,0,0,1}{0,0}{0,0}{2,3}{5,7}{3,4}Returns: {2, 2 }For the first query we want to get a total of 3 from {2,1,3}. For the second query we want to get a total of 4 from {1,3,1}.5
5
{1,1,0,1,2, 1,2,1,1,1, 0,1,2,0,2, 1,2,0,1,1, 1,0,1,1,0}{0,1,0,2,3}{2,1,4,4,3}{1,2,0,1,2}{2,4,4,3,4}{5,2,6,3,1}Returns: {5, 3, 8928, 12, 2 }1
40
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}{0}{0}{0}{39}{20}Returns: {846527587 }There are C(40, 20) = 137846528820 ways to do that. Don't forget mod 1,000,000,009.1
1
{0}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10,2,9,10,15,20,12,7,21,17,13,16,15,9,14,11,12,20,14,7,9,3,19}{55,147,232,244,268,98,102,381,114,251,363,94,31,169,226,24,393,387,8,179,270,283,202,274,264,156,258,54,172,381}Returns: {78093026, 387159947, 0, 0, 291584072, 626572479, 426120834, 0, 0, 0, 745892889, 22065496, 262599170, 0, 7946, 9199037, 0, 54, 142, 103980580, 0, 0, 195571841, 0, 37, 502623060, 0, 0, 0, 96198211 }