Hi,
How to solve Quadruple Counting . I read the editorial but couldn't understand . Any help will be appreciated. Thanks
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hi,
How to solve Quadruple Counting . I read the editorial but couldn't understand . Any help will be appreciated. Thanks
Name |
---|
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.