Hi,
How to solve Quadruple Counting . I read the editorial but couldn't understand . Any help will be appreciated. Thanks
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Hi,
How to solve Quadruple Counting . I read the editorial but couldn't understand . Any help will be appreciated. Thanks
Название |
---|
Its really interesting problem. And interesting solution. It's pretty easy to come with O(n^2) solution, but time limits are really strict, so it won't fit. You need something like O(n*sqrt(max(Ai)) suggested by Editorial to fit (actually it's more like O(n*sqrt(max(Ai))*log(max(Ai))) since we have dp[n][maxA][log(maxA)]). Editorial suggests some mix of divide&conquer (that looks like the one used in simple problem of finding pairs in array that sum up to a given target-value) and DP. Let's look at this function:
From start val[i] initialized with 1s, dp with 0s, and cnt[i] is array with values equal to number of 1s in binary representation of integer "i". "Main loop" goes over every element of array a[] and first divides a[i]<2^16 bitwise into two parts f-first 8 bits of a[i] and s-second 8 bits of a[i]. Than algorithm goes through two loops: "first loop" is taking values from DP and adds them to get result in val[i]; "second loop" puts values into DP.
Now what are those values? For each f (first part of a[i]) and every possible 8bit number (_nextS_ from 0 to 2^8) we put val[i]=1 in dp[][][cnt] corresponding to number of ones in F(f,nextS). And we overwrite val[i] with nwVal, which is either zero or number of variations for i-th element. And in first loop for every possible 8bit number prevF we compute (b[i] — cnt[prevF & f]) — number of ones we need to add up to b[i]. If prevF really was some f(a[j]) (where j<i) then there is some number in DP[prevF][s][] — number of variants for i-th element that satisfy previous steps. And we add all of that numbers and put it back in v[i]. In this way we get number of all i-s for every j in array val[].
Then we run similar function calcOr() and find number of all (i,j) pairs for every k in the same array val[]. And running calcAnd() we get numbers for every l and we add up all those values in final val[] to get final result.