跳到正文

题解

popcount <= 2

qqqaaazzz_qwq约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年6月18日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 3 分钟
获赞
7

版本

  1. v1当前

好玩的题!!

首先我们发现这个限制条件很奇怪,不好分析,因此我们考虑 强制令 a1=0a_1=0,并将答案乘上 2M2^M。

因为 a1a_1 已经是 00 了,所以剩下的数的 popcount\text{popcount} 最多为 22。接下来我们分讨 44 种情况,并分别计数:

  1. 所有数的 popcount≤1\text{popcount} \leq 1,形如 0,2,4,0,2,80,2,4,0,2,8 这样,答案是
(m+1)n−1(m+1)^{n-1}
  1. 只有 11 种 popcount=2\text{popcount}=2 的数,形如 0,5,0,1,4,0,50,5,0,1,4,0,5 这样,答案是
(m2)(4n−1−3n−1)\binom{m}{2} (4^{n-1}-3^{n-1})
  1. 有很多种 popcount=2\text{popcount}=2 的数,并且它们的按位与 ≠0\neq 0,形如 0,5,3,5,1,0,90,5,3,5,1,0,9 这样,答案是
m((m+1)n−1−(m−1)(3n−1−2n−1)−2n−1)m((m+1)^{n-1}-(m-1)(3^{n-1}-2^{n-1})-2^{n-1})
  1. 有恰好 33 种 popcount=2\text{popcount}=2 的数,并且它们的按位与 =0 = 0,形如 0,3,0,5,5,6,0,30,3,0,5,5,6,0,3 这样,答案是
(m3)(4n−1−3×3n−1+3×2n−1−1)\binom{m}{3}(4^{n-1}-3\times3^{n-1}+3\times2^{n-1}-1)

可以证明只有这 44 种情况,将这 44 种答案加起来,就是 a1=0a_1=0 的答案啦~

代码:

C++
#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0)
#define int long long
using namespace std;
const int p = 998244353;

int fpm(int a,int b){
	int c = a, ans = 1;
	while(b){
		if((b&1)==1) ans = (ans*c)%p; 
		c = (c*c)%p;
		b >>= 1;
	}
	return ans;
}

void solve(){
	int n,m;
	cin >> n >> m;
	int ans1 = fpm(m+1,n-1);
	int ans2 = m*(m-1)/2%p*(fpm(4,n-1)-fpm(3,n-1)+p)%p;
	int ans3 = (fpm(m+1,n-1)-(m-1)*(fpm(3,n-1)-fpm(2,n-1))-fpm(2,n-1));
	ans3 = (ans3%p+p)%p;
	ans3 = (ans3*m)%p;
	int ans4 = (fpm(4,n-1)-3*fpm(3,n-1)+3*fpm(2,n-1)-1);
	ans4 = (ans4%p+p)%p;
	ans4 = ans4*m%p*(m-1)%p*(m-2)%p*fpm(6,p-2)%p;
	int res = (ans1+ans2+ans3+ans4)%p;
	res = (res*fpm(2,m))%p;
	cout << res << "\n";
}

signed main()
{
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}
原文