inline Solution f(int k, int first, int m, int d, bool h){ if (k == 1) { returnSolution(0, 1, 0); } int siz = (1 << k) - 1; if (siz < first || !m) { returnSolution(0, 0, 0); } bool flg = k <= lim && h && first + m * d >= siz; if (flg) { if (vis[k][first] == kase) { return F[k][first]; } } int fstbak = first; Solution ans; int sizs = (1 << (k - 1)) - 1; Solution xx; if (first < sizs && m > 0) { int nowm = min(m, (sizs - first - 1) / d + 1); addls(ans, xx = f(k - 1, first, nowm, d, true)); first += nowm * d; m -= nowm; } jian(first, sizs); if (!h) { if (m > 0 && !first) { ans.a += 2, ans.c++; first += d, m--; } jian(first, 1); } if (first < sizs && m > 0) { int nowm = min(m, (sizs - first - 1) / d + 1); addrs(ans, xx = f(k - 1, first, nowm, d, h)); first += nowm * d; m -= nowm; } jian(first, sizs); if (h && m > 0) { ans.b++; } if (flg) { vis[k][fstbak] = kase; F[k][fstbak] = ans; } return ans; }
intmain(){ int k, q; cin >> k >> q; while (q--) { int a, d, m; cin >> a >> d >> m; a--, kase++; Solution ans = f(k, a, m, d, false); cout << ans.a + ans.c << '\n'; } return0; }