Given N numbers(A[0], A[1], ...., A[N-1]) and two prime numbers P, Q. Determine the number of subsets whose product is divisible by P*Q.
2<=N<300, P, Q <= 50, A[i] <= 10,000
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3845 |
2 | jiangly | 3707 |
3 | Benq | 3630 |
4 | orzdevinwang | 3573 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | jqdai0815 | 3532 |
8 | ecnerwala | 3501 |
9 | gyh20 | 3447 |
10 | Rebelz | 3409 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 171 |
2 | awoo | 163 |
3 | adamant | 162 |
4 | maroonrk | 152 |
5 | nor | 151 |
5 | -is-this-fft- | 151 |
7 | atcoder_official | 147 |
7 | TheScrasse | 147 |
9 | Petr | 145 |
10 | pajenegod | 144 |
Given N numbers(A[0], A[1], ...., A[N-1]) and two prime numbers P, Q. Determine the number of subsets whose product is divisible by P*Q.
2<=N<300, P, Q <= 50, A[i] <= 10,000
Название |
---|
Alternative is simple: dp[i][rem]. Where you already consider first i numbers, current remainder by modulo p × q = rem. dp[i][rem] = number of ways to obtaine this configuration. The transitions can be done in p × q, which will produce in final O(npq)
Can you share your implementation? There are lot's of doubt over it.
We create a new array B, where Bi = 0 if it is not divisible by P or Q, 1 if it is divisible by P, 2 if it is divisible by Q, and 3 if it is divisible by both. We want to find the number of subsets that have bitwise OR equal to 3. Then the answer is equal to:
(number of ways to pick at least one element such that Bi = 1) × (number of ways to pick at least one element such that Bi = 2) × (number of ways to pick any number of elements such that Bi = 0) + (number of ways to pick any number of elements such that Bi = 0) × (number of ways to pick any number of elements such that Bi = 1) × (number of ways to pick any number of elements such that Bi = 2) × (number of ways to pick at least one element such that Bi = 3).
Great explanation, but I believe it may not produce correct output when P == Q.
You are correct. In that case, redefine B: Bi = 1 if it is divisible by P, and Bi = 2 if it is divisible by P2. We want to find the number of subsets with sum greater than 1. The solution is very similar to the previous one.
It's seems, like your return 0, which is definitely wrong.
UPD: Missed that P and Q are primes.
P and Q must be primes. Neither 8 nor 1 is prime.