void take(int pos, int no, bits &ret) { ret.set(pos); for(int i=0;i<N-1;i++) { if(rela[i].first == pos && rela[i].second != no) { take2(rela[i].second, ret); } if(rela[i].second == pos && rela[i].first != no) { take2(rela[i].first, ret); } } ret.reset(pos); }
static inline bool iddfs(bits state, val const res, val const rem, val const d) { if (rem == 0) return true; if (memed(state)) return false; if (rem > d) return false; num[d] = res; bits was_processed = state; defrd_size[d] = 0; for (val k = d; k <= start_d; ++k) { val const i = num[k]; for (val j = start_d; j > k; --j) { val const rhs = num[j]; val const sum = i + rhs; if (sum <= max_n && process(was_processed, state, sum, rem, d, add, i, rhs)) return true; val const diff = abs(i - rhs); if (process(was_processed, state, diff, rem, d, sub, max(i, rhs), min(i, rhs))) return true; } for (val j = 2*i, p = 1; j <= max_n; j *= 2, ++p) if (process(was_processed, state, j, rem, d, shift, i, p)) return true; } for (val l = 0; l < defrd_size[d]; ++l) { action const &defrd = deferred[d][l]; state.set(defrd.res); if(iddfs(state, defrd.res, rem, d - 1)) { result[d] = defrd; return true; } state.reset(defrd.res); } return false; }