Let ‘N’ = 2, ‘M’ = 5, ‘F’ = 3 and ‘ARR’ = [2, 4].
Ninja’s account can contain 3 coins ((2 * 4) % 5 = 3), if he chooses Plan 1 and Plan 2. It is enough to remove Plan 1 as he can then only multiply the number of coins in his account by 4.
The first line contains an integer 'T', which denotes the number of test cases.
The next line contains three integers ‘N’, ‘M’ and ‘F’ denoting the number of Plans, Modulo number and coins which Ninja’s bank cannot contain.
The next line contains ‘N’ space-separated integers representing an array of interest factors.
For each test case, print the minimum number of plans you must remove.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 ≤ T ≤ 10
1 ≤ N ≤ 5000
1 ≤ M ≤ 10000
0 ≤ F < M, F ≠ 1
0 ≤ ARR[i] < M
Time limit: 1 sec
The prerequisites to understand this approach are Euler Totient Function, Primitive root and Fermat little theorem.
Here I will be using definitions and results of some theorems in the solution without formal proofs. I have provided the links to these theorems you can go through them before proceeding.
First of all we pay attention to special case when ‘F’ is 0. In this case the answer will be number of zeroes in the array.
A number g is called a primitive root modulo n if every number coprime to n is congruent to the power of g modulo n. Mathematically, g is a primitive root modulo n if and only if for any integer x such that gcd(x,n) = 1, there exists an integer k such that:
gk ≡ x (mod n)
Since our Modulo ‘M’ in this problem is prime then we can find a primitive root for it and write all numbers of array ‘ARR’ as a power of this primitive root R.
Formally speaking, let’s replace each element in array ARR : ARR[i] = f(ARR[i]) such that RARR[i] ≡ ARR[i] (mod M)
In this case let’s suppose we picked a set of ‘K’ plans {X1, X2, .. Xk,} then:
X1 * X2 * .. * Xk ≡ Rf(x1) + f(x2) + .. + f(xk)
According to Fermal Little Theorem if ‘M’ is prime then:
aM ≡ a (mod M)
ax ≡ ax mod (M - 1) (mod M)
Now,
X1 * X2 * .. * Xk ≡ F(mod M) if and only if f(X1) + f(X2) + .. + f(Xk) = f(F)(mod (M - 1))
where ‘F’ is coins which Ninja’s bank cannot contain.
So after transforming the array with our function f we need to remove the minimum number of elements such that it’s impossible to form a multiset with the numbers such that the sum of this multiset’s elements is equal to f(F) (modulo M - 1).
We need to remove the minimum number of elements from our array ‘ARR’ such that gcd(elements in ARR) is not divisible by f(F).
So let’s fix our final GCD of the array let’s call it Garr and for each possible value we count all multiples of Garr so we keep them in array and remove rest of elements. After examining all possible values of Garr we take best choice.
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers