[ABC246F] typewriter
Problem Statement
We have a typewriter with $N$ rows. The keys in the $i$-th row from the top can type the characters in a string $S_i$.
Let us use this keyboard to enter a string, as follows.
- First, choose an integer $1 \le k \le N$.
- Then, start with an empty string and only use the keys in the $k$-th row from the top to enter a string of length exactly $L$.
How many strings of length $L$ can be entered in this way? Since the answer can be enormous, print it modulo $998244353$.
Constraints
- $N$ and $L$ are integers.
- $1 \le N \le 18$
- $1 \le L \le 10^9$
- $S_i$ is a (not necessarily contiguous) non-empty subsequence of
abcdefghijklmnopqrstuvwxyz
.
Input
Input is given from Standard Input in the following format:
$N$ $L$ $S_1$ $S_2$ $\dots$ $S_N$
Output
Print the answer.
Sample Input 1
2 2 ab ac
Sample Output 1
7
We can enter seven strings: aa
, ab
, ac
, ba
, bb
, ca
, cc
.
Sample Input 2
4 3 abcdefg hijklmnop qrstuv wxyz
Sample Output 2
1352
Sample Input 3
5 1000000000 abc acde cefg abcfh dghi
Sample Output 3
346462871
Be sure to print the answer modulo $998244353$.
如果只有一个串,这一个串总共有 \(x\) 个字符,那么构成的长度为 \(l\) 的串总共有 \(x^l\) 个。
然后我们很快发现会有重复的,那么就需要容斥原理。假设有 \(k\) 个串,设在每个串中都出现了的字符数量为 \(s\),那么他们共同重复的串为 \(s^l\)
然后容斥就可以了。
#include<bits/stdc++.h>
const int N=35,P=998244353;
char s[N];
int t[N],n,len,l;
long long ans;
int pow(int x,int y)
{
if(!y)
return 1;
int t=pow(x,y>>1);
if(y&1)
return 1LL*t*t%P*x%P;
return 1LL*t*t%P;
}
int mo(int x)
{
return (x%P+P)%P;
}
int bitcnt(int x)
{
int cnt=0;
while(x)
{
x-=x&-x;
++cnt;
}
return cnt;
}
void dfs(int x,int y,int z)
{
if(x>n)
{
if(y)
{
int f=y&1? 1:-1;
ans=mo(ans+f*pow(bitcnt(z),l)%P);
}
}
else
{
dfs(x+1,y+1,z&t[x]);
dfs(x+1,y,z);
}
}
int main()
{
scanf("%d%d",&n,&l);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
len=strlen(s+1);
for(int j=1;j<=len;j++)
t[i]|=1<<s[j]-'a';
// printf("%d\n",t[i]);
}
dfs(1,0,(1<<26)-1);
printf("%lld",ans);
}