[ABC246F] typewriter
Problem Statement
We have a typewriter with rows. The keys in the -th row from the top can type the characters in a string .
Let us use this keyboard to enter a string, as follows.
- First, choose an integer .
- Then, start with an empty string and only use the keys in the -th row from the top to enter a string of length exactly .
How many strings of length can be entered in this way? Since the answer can be enormous, print it modulo .
Constraints
- and are integers.
- 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 .
如果只有一个串,这一个串总共有 个字符,那么构成的长度为 的串总共有 个。
然后我们很快发现会有重复的,那么就需要容斥原理。假设有 个串,设在每个串中都出现了的字符数量为 ,那么他们共同重复的串为
然后容斥就可以了。
#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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现