#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=35; vector<pair<int, int>> swaps; int ans[MAXN+2]; set<ll> S; void recur(ll msk, int m) { if(m==-1){ S.insert(msk); return; } int a = swaps[m].first; int b = swaps[m].second; a--; b--; if((msk & (1ll<<a)) && !(msk & (1ll<<b))) return; if(!(msk & (1ll<<a)) && (msk & (1ll<<b))){ ll nmsk = msk ^ (1ll<<a) ^ (1ll<<b); recur(nmsk, m-1); recur(msk, m-1); return; } recur(msk, m-1); return; } void solve1(int n, int m) { for(int msk=1;msk<(1<<n);msk++){ int M = msk; int cnt=__builtin_popcount(M); for(int i=0;i<m;i++){ int a = swaps[i].first; int b = swaps[i].second; a--; b--; if((M & (1<<a)) && !(M & (1<<b))){ M^=(1<<a); M^=(1<<b); } } bool czy_start=0, czy_dobre=1; int cnt2=0; for(int i=0;i<n;i++){ if(M & (1<<i)){ czy_start=1; cnt2++; if(cnt2==cnt) break; } else if(czy_start){ czy_dobre=0; break; } } ans[cnt]^=czy_dobre; } for(int i=1;i<=n;i++){ cout << ans[i] << " "; } } void solve2(int n, int m) { for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ ll msk=0; for(int k=i;k<=j;k++){ msk^=(1ll<<k); } recur(msk, m-1); ans[j-i+1]+=S.size(); ans[j-i+1]%=2; S.clear(); } } for(int i=1;i<=n;i++){ cout<<ans[i]<<" "; } } void solve() { int n,m; cin >> n >> m; for(int i=0;i<m;i++){ int a,b; cin >> a >> b; swaps.push_back({a, b}); } if(n <= 10) solve1(n, m); else solve2(n, m); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=35; vector<pair<int, int>> swaps; int ans[MAXN+2]; set<ll> S; void recur(ll msk, int m) { if(m==-1){ S.insert(msk); return; } int a = swaps[m].first; int b = swaps[m].second; a--; b--; if((msk & (1ll<<a)) && !(msk & (1ll<<b))) return; if(!(msk & (1ll<<a)) && (msk & (1ll<<b))){ ll nmsk = msk ^ (1ll<<a) ^ (1ll<<b); recur(nmsk, m-1); recur(msk, m-1); return; } recur(msk, m-1); return; } void solve1(int n, int m) { for(int msk=1;msk<(1<<n);msk++){ int M = msk; int cnt=__builtin_popcount(M); for(int i=0;i<m;i++){ int a = swaps[i].first; int b = swaps[i].second; a--; b--; if((M & (1<<a)) && !(M & (1<<b))){ M^=(1<<a); M^=(1<<b); } } bool czy_start=0, czy_dobre=1; int cnt2=0; for(int i=0;i<n;i++){ if(M & (1<<i)){ czy_start=1; cnt2++; if(cnt2==cnt) break; } else if(czy_start){ czy_dobre=0; break; } } ans[cnt]^=czy_dobre; } for(int i=1;i<=n;i++){ cout << ans[i] << " "; } } void solve2(int n, int m) { for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ ll msk=0; for(int k=i;k<=j;k++){ msk^=(1ll<<k); } recur(msk, m-1); ans[j-i+1]+=S.size(); ans[j-i+1]%=2; S.clear(); } } for(int i=1;i<=n;i++){ cout<<ans[i]<<" "; } } void solve() { int n,m; cin >> n >> m; for(int i=0;i<m;i++){ int a,b; cin >> a >> b; swaps.push_back({a, b}); } if(n <= 10) solve1(n, m); else solve2(n, m); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |