triples II

题目链接

题意

计算有多少个长度为$n$的子序列或起来的结果是$a$,且每个数字都是3的倍数。
3的倍数的数字很容易在$ \log$的时间里dp出来,考虑要求它们或的结果是$a$。
容斥一下就可以了。但是分析一下复杂度就会意识到有问题。
我们直接按照子集大小容斥一定会超时。
其实容斥的思路,就是去找个数,我们知道3的倍数取决于它有多少个对3余数为1的位和多少余2的位,和这些位具体是什么其实无关。
抓住这个特点,我们可以把许多子集一起处理。
枚举有$i$位模3为1,$j$位模3为2,然后组合数一下就可以容斥这一“类”子集。

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
#include<bits/stdc++.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 63;
int C[N][N],f[N][N];
int main() {
REP(i,0,N-1) {
C[i][0] = 1;
REP(j,1,i) C[i][j]=(C[i-1][j]+C[i-1][j-1])%P;
}
REP(i,0,N-1) REP(j,0,N-1) REP(ii,0,i) REP(jj,0,j) if ((ii+2*jj)%3==0) {
f[i][j] = (f[i][j]+(ll)C[i][ii]*C[j][jj])%P;
}
int t;
scanf("%d", &t);
while (t--) {
ll n, a;
scanf("%lld%lld", &n, &a);
n %= P-1;
int c[2]{};
REP(i,0,N-1) if (a>>i&1) ++c[i&1];
int ans = 0;
REP(i,0,c[0]) REP(j,0,c[1]) {
int ret = (ll)C[c[0]][i]*C[c[1]][j]%P*qpow(f[i][j],n)%P;
if (c[0]+c[1]-i-j&1) ret = P-ret;
ans = (ans+ret)%P;
}
printf("%d\n", ans);
}
}

0%