[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 Si.

Let us use this keyboard to enter a string, as follows.

  • First, choose an integer 1kN.
  • 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.
  • 1N18
  • 1L109
  • Si 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 的串总共有 xl 个。

然后我们很快发现会有重复的,那么就需要容斥原理。假设有 k 个串,设在每个串中都出现了的字符数量为 s,那么他们共同重复的串为 sl

然后容斥就可以了。

#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);
}
posted @   灰鲭鲨  阅读(76)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示