Hi,
How to solve Quadruple Counting . I read the editorial but couldn't understand . Any help will be appreciated. Thanks
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3880 |
2 | jiangly | 3669 |
3 | ecnerwala | 3654 |
4 | Benq | 3627 |
5 | orzdevinwang | 3612 |
6 | Geothermal | 3569 |
6 | cnnfls_csy | 3569 |
8 | jqdai0815 | 3532 |
9 | Radewoosh | 3522 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | awoo | 161 |
2 | maomao90 | 160 |
3 | adamant | 156 |
4 | maroonrk | 153 |
5 | -is-this-fft- | 148 |
5 | atcoder_official | 148 |
5 | SecondThread | 148 |
8 | Petr | 147 |
9 | nor | 144 |
9 | TheScrasse | 144 |
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.