Problem Statement
One day, Bob the Coder was wondering whether abstract programming problems can have applications in practice. The next day, he was selected to be on a quiz show. He will win one million dollars if he answers the following question:
Given a int[] A with N elements and an int K, count the number of tuples (p, q, r) such that 0 <= p < q < r < N and A[p] * A[q] * A[r] is divisible by K.
Please compute and return the answer to Bob's question.
Given a int[] A with N elements and an int K, count the number of tuples (p, q, r) such that 0 <= p < q < r < N and A[p] * A[q] * A[r] is divisible by K.
Please compute and return the answer to Bob's question.
Definition
- Class:
- AnArray
- Method:
- solveProblem
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int solveProblem(int[] A, int K)
- (be sure your method is public)
Constraints
- A will contain between 3 and 2,000 elements, inclusive.
- K will be between 1 and 1,000,000, inclusive.
- Each element of A will be between 1 and 100,000,000, inclusive.
Examples
{31, 1, 3, 7, 2, 5}30
Returns: 1
The return value is 1 because there is exactly one valid tuple. The tuple is (2, 4, 5). It is valid because A[2] * A[4] * A[5] = 3 * 2 * 5 = 30.{4, 5, 2, 25}100
Returns: 2
{100000000, 100000000, 100000000}1000000
Returns: 1
Note that the product A[p] * A[q] * A[r] doesn't have to fit into a 64-bit integer variable.{269, 154, 94, 221, 171, 154, 50, 210, 258, 358, 121, 159, 8, 47, 290, 125, 291, 293, 338, 248, 295, 160, 268, 227, 99, 4, 273}360
Returns: 114
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}1
Returns: 220