forked from ncduy0303/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOS DP.cpp
49 lines (42 loc) · 1.12 KB
/
SOS DP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Sum-over-subset dp to optimise from O(4^n) or O(3^n) to O(n*2^n)
// Problem link: https://cses.fi/problemset/task/1654
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const int MAX_M = 20;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
int inv(int x) {return x ^ ((1 << MAX_M) - 1);}
void solve() {
int n; cin >> n;
vector<int> a(n);
vector<int> dp(1 << MAX_M), dp_inv(1 << MAX_M);
for (int &x : a) {
cin >> x;
dp[x]++;
dp_inv[inv(x)]++;
}
for (int i = 0; i < MAX_M; i++) {
for (int mask = 0; mask < (1 << MAX_M); mask++) {
if (mask & (1 << i)) {
dp[mask] += dp[mask ^ (1 << i)];
dp_inv[mask] += dp_inv[mask ^ (1 << i)];
}
}
}
for (int x : a) {
cout << dp[x] << " " << dp_inv[inv(x)] << " " << n - dp[inv(x)] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}