loj #547. 「LibreOJ β Round #7」匹配字符串
#547. 「LibreOJ β Round #7」匹配字符串
题目描述
对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 mmm 的子串 s′s's′,不为全 1 串。
请求出所有长度为 nnn 的 01 串中,有多少合法的串,答案对 655376553765537 取模。
输入格式
输入共一行,包含两个正整数 n,mn,mn,m。
输出格式
输出共一行,表示所求的和对 655376553765537 取模的结果。
样例
样例输入 1
5 2
样例输出 1
13
样例解释 1
以下是所有合法的串:
00000
00001
00010
00100
00101
01000
01001
01010
10000
10001
10010
10100
10101
样例输入 2
2018 7
样例输出 2
27940
#include<iostream>
#include<cstdio>
#include<cstring>
#define mod 65537
using namespace std;
int n,m;
struct node{
int n,m;
int a[][];
node(){memset(a,,sizeof(a));}
node operator * (const node &b)const{
node res;
res.n=n;res.m=b.m;
for(int i=;i<=n;i++)
for(int j=;j<=b.m;j++)
for(int k=;k<=m;k++)
res.a[i][j]+=1LL*a[i][k]*b.a[k][j]%mod;
return res;
}
};
bool check(int sta){
int cnt=;
for(int i=;i<=n;i++){
if(sta&(<<i-))cnt++;
else cnt=;
if(cnt>=m)return ;
}
return ;
}
node Pow(node x,int y){
node res;
res.n=;res.m=;
res.a[][]=;res.a[][]=;
while(y){
if(y&)res=res*x;
x=x*x;
y>>=;
}
return res;
}
void work1(){
node a;
a.n=;a.m=;
a.a[][]=;a.a[][]=;
node b;
b.n=b.m=;
b.a[][]=;b.a[][]=;b.a[][]=;
b=Pow(b,n-);
a=a*b;
int ans=(a.a[][]+a.a[][])%mod;
printf("%d",ans);
}
int main(){
scanf("%d%d",&n,&m);
if(m==){puts("");return ;}
if(m==){work1();return ;}
int ans=;
for(int sta=;sta<(<<n);sta++)
if(check(sta)){
ans++;
if(ans>=mod)ans-=mod;
}
printf("%d",ans);
return ;
}
13分 矩阵快速幂优化dp+枚举
#include<iostream>
#include<cstdio>
#include<cstring>
#define mod 65537
using namespace std;
long long n,m;
int tmp[],b[],c[],ans,inv[mod],fac[mod],bin[];
int Pow(int x,int y){
int res=;
while(y){
if(y&)res=1LL*res*x%mod;
x=1LL*x*x%mod;
y>>=;
}
return res;
}
int C(int n,int m){
if(m>n)return ;
return 1LL*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int Lucas(long long n,long long m){
if(!m)return ;
return 1LL*Lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod;
}
void mul(int *b,int *c){
for(int i=;i<m;i++)
for(int j=;j<m;j++)
tmp[i+j]=(tmp[i+j]+1LL*b[i]*c[j]%mod)%mod;
for(int i=*m-;i>=m;i--)
for(int j=;j<=m;j++)
tmp[i-j]=(tmp[i-j]+tmp[i])%mod;
for(int i=;i<m;i++)b[i]=tmp[i],tmp[i]=tmp[i+m]=;
}
void solve1(){
bin[]=c[]=;
if(m==)b[]=;
else b[]=;
for(int i=;i<m;i++)
bin[i]=1LL**bin[i-]%mod;
while(n){
if(n&)mul(c,b);
mul(b,b);
n>>=;
}
for(int i=;i<m;i++)
ans=(ans+1LL*bin[i]*c[i]%mod)%mod;
cout<<ans;
}
int s(long long n){
long long base=Pow(Pow(,m+),mod-);
long long cc=Pow(,n);int res=;
for(int k=;k*(m+)<=n;k++){
long long tt=1LL*Lucas(n-k*m,k)*cc%mod;
tt=(k&)?mod-tt:tt;
res=(res+tt)>=mod?res-mod+tt:res+tt;
cc=1LL*cc*base%mod;
}
return res;
}
void solve2(){
fac[]=fac[]=;
for(int i=;i<mod;i++)fac[i]=1LL*fac[i-]*i%mod;
inv[mod-]=mod-;
for(int i=mod-;i>=;i--)inv[i-]=1LL*inv[i]*i%mod;
ans=s(n+)-s(n);
printf("%d\n",(ans<)?ans+mod:ans);
}
int main(){
cin>>n>>m;
if(m<=)solve1();
else solve2();
return ;
}
100分
loj #547. 「LibreOJ β Round #7」匹配字符串的更多相关文章
- [LOJ#531]「LibreOJ β Round #5」游戏
[LOJ#531]「LibreOJ β Round #5」游戏 试题描述 LCR 三分钟就解决了问题,她自信地输入了结果-- > -- 正在检查程序 -- > -- 检查通过,正在评估智商 ...
- [LOJ#530]「LibreOJ β Round #5」最小倍数
[LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...
- [LOJ#516]「LibreOJ β Round #2」DP 一般看规律
[LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...
- [LOJ#515]「LibreOJ β Round #2」贪心只能过样例
[LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...
- [LOJ#525]「LibreOJ β Round #4」多项式
[LOJ#525]「LibreOJ β Round #4」多项式 试题描述 给定一个正整数 k,你需要寻找一个系数均为 0 到 k−1 之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x) ...
- [LOJ#526]「LibreOJ β Round #4」子集
[LOJ#526]「LibreOJ β Round #4」子集 试题描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两 ...
- [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)
[LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机) 试题描述 IOI 的比赛开始了.Jsp 和 Rlc 坐在一个角落,这时他们听到了一个异样的声音 …… 接着他们发现自己收 ...
- loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分
$ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinob ...
- Loj #528. 「LibreOJ β Round #4」求和 (莫比乌斯反演)
题目链接:https://loj.ac/problem/528 题目:给定两个正整数N,M,你需要计算ΣΣu(gcd(i,j))^2 mod 998244353 ,其中i属于[1,N],j属于[1,M ...
随机推荐
- 【转】IMSI和IMEI
国际移动客户识别码(International Mobile Subscriber Identification Number) 为了在无线路径和整个GSM移动通信网上正确地识别某个移动客户,就必须给 ...
- 关于mybatis中基本类型条件判断问题
零:sql动态语句中经常会有根据数据库某个字段状态进行判断的 如:status=0为未激活,status=1为激活的,那搜索未激活时: <if test="model.activeSt ...
- Java-API:java.util.regex.Pattern
ylbtech-Java-API:java.util.regex.Pattern 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://docs. ...
- Task Crontab
Crontab 1.查看任务 crontab -l 2.编辑任务 1)对应用户登录后编辑其下的作业 crontab -e 2)删除指定用户任务 crontab -u user -r 3)删除用户下指定 ...
- 实验吧CTF题库-编程(部分)
百米 3秒提交答案,数字是随机变化的 利用Python脚本解题 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import ...
- docker 安装mysql 使用navicat访问 解决
1. 下载Mysql的Docker镜像: 2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306.测试过程没有挂载本地数据盘: 3. 查看已运行 ...
- 问题:System.Guid.NewGuid();结果:C# System.Guid.NewGuid()
C# System.Guid.NewGuid() 概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique I ...
- executeUpdate,executeQuery,executeBatch 的区别
executeQuery : 用于实现单个结果集,例如: Select 一般使用executeQuery 就是来实现单个结果集的工具 executeUpdate 用于执行 INSERT.UPDATE ...
- ubuntu16部署gitlab
一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...
- sql 一些偶尔会用到的写法和函数 不定时更新
小数转整数: --round() 遵循四舍五入把原值转化为指定小数位数,如: ) -- =1 ) -- =2 --floor() 向下舍入为指定小数位数 如: SELECT floor(1.45) - ...