Problem Statement
In this problem, you will be given an int N representing the number of digits the number has, and a prime number P. Your task is to find a divisibility criterion for a number with N digits with a prime P. More specifically, you should assign a factor for each digit, such that their sum is divisible by P if and only if the N-digit number is divisible by P.
In order to ensure the uniqueness of the solution, we add the following restrictions:
- all the factors must be between 0 and P-1 inclusive.
- the factor for the rightmost digit should be 1.
- if the N-digit number has leading zeros, the returned factors should still work.
Return a int[] representing the factors assigned to each digit, starting with the leftmost digit.
Definition
- Class:
- DivisibilityCriteria
- Method:
- multipliers
- Parameters:
- int, int
- Returns:
- int[]
- Method signature:
- int[] multipliers(int N, int P)
- (be sure your method is public)
Constraints
- P is a prime number less than 100.
- N is between 1 and 18, inclusive.
Examples
6
7
Returns: {5, 4, 6, 2, 3, 1 }
We know that the rightmost digit should be multiplied by a factor of 1. Now we want to find the factor for the next digit. Let's consider the number 35, which is divisible by 7. The factor for digit '3' can now be found from the following formula: 3 * x + 5 * 1 mod 7 = 0, where x is between 0 and 6 (see the problem statement). This leads us to the unique solution, x=3. To find the next factor, we take a three digit number which is divisible by 7, let's say 532. If we denote by y the factor of '5', we should have: 5 * y + 3 * 3 + 2 * 1 mod 7 = 0. This leads us to the unique solution, y=2. Proceeding analogously for the next digits of the number, we obtain successively the factors 6, 4 and 5.
7
11
Returns: {1, 10, 1, 10, 1, 10, 1 }
5
13
Returns: {3, 12, 9, 10, 1 }
2
2
Returns: {0, 1 }
Note that only the rightmost digit counts when assessing the divisibility with 2, so if the number has more than one digit, all the other factors are 0.
16
97
Returns: {45, 53, 15, 50, 5, 49, 34, 81, 76, 27, 90, 9, 30, 3, 10, 1 }
18
5
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
18
17
Returns: {10, 1, 12, 8, 11, 13, 3, 2, 7, 16, 5, 9, 6, 4, 14, 15, 10, 1 }
8
3
Returns: {1, 1, 1, 1, 1, 1, 1, 1 }
2
19
Returns: {10, 1 }
18
2
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
18
31
Returns: {7, 10, 1, 28, 9, 4, 19, 5, 16, 14, 20, 2, 25, 18, 8, 7, 10, 1 }
18
59
Returns: {8, 48, 52, 17, 43, 22, 14, 25, 32, 15, 31, 9, 54, 29, 56, 41, 10, 1 }
18
71
Returns: {8, 15, 37, 25, 38, 18, 16, 30, 3, 50, 5, 36, 32, 60, 6, 29, 10, 1 }
18
83
Returns: {61, 31, 28, 36, 70, 7, 9, 59, 64, 23, 77, 16, 68, 40, 4, 17, 10, 1 }
1
83
Returns: {1 }
1
89
Returns: {1 }
15
41
Returns: {37, 16, 18, 10, 1, 37, 16, 18, 10, 1, 37, 16, 18, 10, 1 }
12
23
Returns: {22, 16, 20, 2, 14, 6, 19, 18, 11, 8, 10, 1 }
15
29
Returns: {28, 26, 20, 2, 6, 18, 25, 17, 22, 8, 24, 14, 13, 10, 1 }
10
13
Returns: {12, 9, 10, 1, 4, 3, 12, 9, 10, 1 }
2
5
Returns: {0, 1 }
1
5
Returns: {1 }
18
47
Returns: {5, 24, 40, 4, 38, 32, 22, 21, 35, 27, 45, 28, 31, 36, 13, 6, 10, 1 }
18
53
Returns: {36, 46, 47, 10, 1, 16, 44, 15, 28, 24, 13, 49, 42, 36, 46, 47, 10, 1 }
17
67
Returns: {39, 24, 56, 19, 22, 29, 23, 9, 21, 49, 25, 36, 17, 62, 33, 10, 1 }
2
79
Returns: {10, 1 }
18
2
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
18
3
Returns: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
18
5
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
18
7
Returns: {5, 4, 6, 2, 3, 1, 5, 4, 6, 2, 3, 1, 5, 4, 6, 2, 3, 1 }
18
11
Returns: {10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1 }
18
13
Returns: {4, 3, 12, 9, 10, 1, 4, 3, 12, 9, 10, 1, 4, 3, 12, 9, 10, 1 }
18
17
Returns: {10, 1, 12, 8, 11, 13, 3, 2, 7, 16, 5, 9, 6, 4, 14, 15, 10, 1 }
18
19
Returns: {2, 4, 8, 16, 13, 7, 14, 9, 18, 17, 15, 11, 3, 6, 12, 5, 10, 1 }
18
23
Returns: {17, 4, 5, 12, 15, 13, 22, 16, 20, 2, 14, 6, 19, 18, 11, 8, 10, 1 }
18
29
Returns: {15, 16, 19, 28, 26, 20, 2, 6, 18, 25, 17, 22, 8, 24, 14, 13, 10, 1 }
18
31
Returns: {7, 10, 1, 28, 9, 4, 19, 5, 16, 14, 20, 2, 25, 18, 8, 7, 10, 1 }
18
37
Returns: {26, 10, 1, 26, 10, 1, 26, 10, 1, 26, 10, 1, 26, 10, 1, 26, 10, 1 }
18
41
Returns: {18, 10, 1, 37, 16, 18, 10, 1, 37, 16, 18, 10, 1, 37, 16, 18, 10, 1 }
18
43
Returns: {9, 31, 16, 36, 38, 21, 15, 23, 41, 17, 6, 35, 25, 24, 11, 14, 10, 1 }
18
47
Returns: {5, 24, 40, 4, 38, 32, 22, 21, 35, 27, 45, 28, 31, 36, 13, 6, 10, 1 }
18
53
Returns: {36, 46, 47, 10, 1, 16, 44, 15, 28, 24, 13, 49, 42, 36, 46, 47, 10, 1 }
18
59
Returns: {8, 48, 52, 17, 43, 22, 14, 25, 32, 15, 31, 9, 54, 29, 56, 41, 10, 1 }
18
61
Returns: {59, 12, 50, 5, 31, 58, 18, 14, 38, 16, 26, 27, 21, 57, 24, 39, 10, 1 }
18
67
Returns: {55, 39, 24, 56, 19, 22, 29, 23, 9, 21, 49, 25, 36, 17, 62, 33, 10, 1 }
18
71
Returns: {8, 15, 37, 25, 38, 18, 16, 30, 3, 50, 5, 36, 32, 60, 6, 29, 10, 1 }
18
73
Returns: {10, 1, 22, 46, 63, 72, 51, 27, 10, 1, 22, 46, 63, 72, 51, 27, 10, 1 }
18
79
Returns: {46, 52, 21, 10, 1, 8, 64, 38, 67, 62, 22, 18, 65, 46, 52, 21, 10, 1 }
18
83
Returns: {61, 31, 28, 36, 70, 7, 9, 59, 64, 23, 77, 16, 68, 40, 4, 17, 10, 1 }
18
89
Returns: {47, 67, 69, 87, 71, 16, 55, 50, 5, 45, 49, 85, 53, 32, 21, 11, 10, 1 }
18
97
Returns: {38, 62, 45, 53, 15, 50, 5, 49, 34, 81, 76, 27, 90, 9, 30, 3, 10, 1 }
18
97
Returns: {38, 62, 45, 53, 15, 50, 5, 49, 34, 81, 76, 27, 90, 9, 30, 3, 10, 1 }
6
7
Returns: {5, 4, 6, 2, 3, 1 }
16
97
Returns: {45, 53, 15, 50, 5, 49, 34, 81, 76, 27, 90, 9, 30, 3, 10, 1 }
18
7
Returns: {5, 4, 6, 2, 3, 1, 5, 4, 6, 2, 3, 1, 5, 4, 6, 2, 3, 1 }
18
11
Returns: {10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1 }