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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

/*************************************************************************/

ll binom(int n, int k) {
    ll ans = 1;
    FOR(i,1,k) {
        ans *= (n - i + 1);
        ans /= i;
    }
    
    return ans;
}

const int LG = 14;
VI moves[LG];

void prepare() {
    FOR(i,0,LG-1) FOR(j,0,i) if (binom(i, j) % 2) {
        moves[i].pb(j);
    }
}

/*************************************************************************/

struct VecHash {
    size_t operator() (const VI &x) const {
        size_t ans = 0;
        for (int val : x) {
            ans += val * val * val;
        }
        
        return ans;
    }
};

typedef unordered_set <VI,VecHash> hashset;

/*************************************************************************/

void xorInsert(hashset &s, const VI &elem) {
    auto it = s.find(elem);
    
    if (it == s.end()) {
        s.insert(elem);
    } else {
        s.erase(it);
    }
}

void choose(int i, VI &sol, int n, int t, vector <hashset> &solutions) {
    if (i == t) {
        VI temp = sol;
        
        sort(temp.begin(), temp.end());
        xorInsert(solutions[temp.size()], temp);
        
        return ;
    } else if (!(n & (1 << i))) {
        choose(i + 1, sol, n, t, solutions);
        return ;
    }

    for (int j : moves[i]) {
        sol.pb(j);
        choose(i + 1, sol, n, t, solutions);

        sol.pop_back();
    }
}

/*************************************************************************/

VI solve(int n, int subn, const VVI &subq, VVI &nextQ) {
    int t = 0;
    while ((1 << t) <= n) t++;
    
    vector <hashset> q(t+1);
    
    for (VI s : subq) {
        choose(0, s, n ^ subn, t, q);
    }
    
    VI ans(n,0);
    
    FORD(j,t,0) for (auto e : q[j]) {
        int ind = -1;
        FOR(i,0,SIZE(e)-2) if (e[i] == e[i+1]) {
            ind = i;
            break;
        }
        
        if (ind == -1) {
            int cost = n;
            for (int type : e) {
                cost -= (1 << type);
            }
            
            ans[cost]++;
            
            if (e.size() > 1) {
                ans[cost + (1 << e[0])]++;
            }

            nextQ.pb(e);
            continue;
        }
        
        e.erase(e.begin() + ind);
        
        auto f = e;
        while (ind < SIZE(f)-1 && f[ind] == f[ind + 1]) {
            ind++;
        }
        
        f[ind]++;
        
        xorInsert(q[e.size()], e);
        xorInsert(q[f.size()], f);
    }

    FOR(i,0,n-1) {
        ans[i] %= 2;
    }
    
    return ans;
}

/*************************************************************************/

const int MAXN = (1 << 14);

int main() {
    ios_base::sync_with_stdio(0);
    prepare();
    
    int q;
    cin >> q;
    
    VVP queries(MAXN);
    
    FOR(i,0,q-1) {
        int n, k;
        cin >> n >> k;
        
        queries[n].pb({k, i});
    }
    
    VI ns;
    FOR(n,0,MAXN-1) if (!queries[n].empty()) {
        ns.pb(n);
    }
    
    VI ans(q);
    vector <VVI> subqs(ns.size());
    
    FORE(i,ns) {
        int n = ns[i];
        
        int bestIndex = -1;
        int bestDiff = __builtin_popcount(n);
        
        FOR(j,0,i-1) if ((ns[j] & n) == ns[j]) {
            int diff = __builtin_popcount(ns[j] ^ n);
        
            if (diff < bestDiff) {
                bestIndex = j;
                bestDiff = diff;
            }
        }

        int subn = (bestIndex == -1 ? 0 : ns[bestIndex]);
        VVI subq = (bestIndex == -1 ? VVI {{}} : subqs[bestIndex]);
            
        VI result = solve(n, subn, subq, subqs[i]);
        
        for (auto p : queries[n]) {
            ans[p.nd] = result[p.st];
        }
    }
    
    int totalSize = 0;
    for (auto &e : subqs) totalSize += SIZE(e);
    
    FOR(i,0,q-1) {
        cout << ans[i] << endl;
    }

    return 0;
}

/*************************************************************************/