算法分析
本题是一道有 $\operatorname{ARC}$ 味道的题,大概可以放在 $C$ 题?
结论:
连通块大小一定为 $2^{k+1}-1$, $k \in \mathbb{N}^{+}$
对于第 $k$ 列的元素,$01$ 序列每隔 $2^k$ 个交替一次,因此长度为 $2^k$。对于这一列,从右往左一列的同颜色格子数为 $2^k, 2^{k-1}, \cdots, 2^0$,总和为 $2^{k+1}-1$ 。
计算连通块大小的过程启示我们寻找 $(x, y)$ 对应的最高点。
显然 $x$ 的低 $y$ 位没有用,可以通过按位右移移除。注意特判 $2^y > x$ 的情况。
问题转化为寻找 $x$ 从最低位开始有多少个连续的 0/1
。$1$ 的情况可以通过给原数 +1
来转化成 $0$ 的情况,可以发现 $0$ 的个数就是 $\operatorname{ctz}(x)$ 。
答案即为 $2^{p+y}-1$。
C++ 代码
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const int mod = 998244353;
//const int mod = 1000000007;
struct mint {
ll x;
mint(ll x=0):x((x%mod+mod)%mod) {}
mint operator-() const {
return mint(-x);
}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
return mint(*this) += a;
}
mint operator-(const mint a) const {
return mint(*this) -= a;
}
mint operator*(const mint a) const {
return mint(*this) *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return *this *= a.inv();
}
mint operator/(const mint a) const {
return mint(*this) /= a;
}
};
istream& operator>>(istream& is, mint& a) {
return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
return os << a.x;
}
int main() {
ll n; int q;
cin >> n >> q;
while (q--) {
ll x, y;
cin >> x >> y;
mint ans;
if (y > 60 or x < 1ll<<y) ans = mint(2).pow(n);
else {
x >>= y;
x += x&1;
ans = mint(2).pow(y+__builtin_ctzll(x));
}
ans -= 1;
cout << ans << '\n';
}
return 0;
}