CF585F Digits of Number Pi
把\(s\)串所有长度为\(\lfloor \frac{d}{2}\rfloor\)的子串插入一个ACAM中,之后数位dp就好了,状态是\(dp_{i,j,0/1}\)第\(i\)位,在ACAM上的节点\(j\),不卡/卡上界;正难则反一下,统计所有不能被表示即没有经过结束标记的路径即可
注意前导0的处理
代码
#include<bits/stdc++.h>
#define re register
#define LL long long
const int mod=1e9+7;
char S[1005],A[55],B[55];int d;
int fa[1005*50],son[1005*50][10],f[1005*50],cnt;
int dp[2][1005*25][2],n,lm[55];
inline int dqm(int x) {return x<0?x+mod:x;}
inline int qm(int x) {return x>=mod?x-mod:x;}
inline void ins(int l,int r) {
int nw=0;
for(re int i=l;i<=r;++i) {
if(!son[nw][S[i]-'0']) son[nw][S[i]-'0']=++cnt;
nw=son[nw][S[i]-'0'];
}
f[nw]=1;
}
inline int solve(char *H) {
int ans=0;
for(re int i=1;i<=d;i++) lm[i]=H[d-i+1]-'0';
for(re int i=d;i;i--) ans=10ll*ans%mod,ans=qm(ans+lm[i]);
int o=0;memset(dp,0,sizeof(dp));dp[0][0][1]=1;
for(re int i=d;i;--i,o^=1) {
memset(dp[o^1],0,sizeof(dp[o^1]));
for(re int j=0;j<=cnt;++j) {
if(!dp[o][j][0]) continue;
for(re int k=0;k<=9;++k) {
if(f[son[j][k]]) continue;
dp[o^1][son[j][k]][0]=qm(dp[o^1][son[j][k]][0]+dp[o][j][0]);
}
}
for(re int j=0;j<=cnt;++j) {
if(!dp[o][j][1]) continue;
for(re int k=0;k<lm[i];++k) {
if(f[son[j][k]]) continue;
dp[o^1][son[j][k]][0]=qm(dp[o^1][son[j][k]][0]+dp[o][j][1]);
}
if(!f[son[j][lm[i]]])
dp[o^1][son[j][lm[i]]][1]=qm(dp[o^1][son[j][lm[i]]][1]+dp[o][j][1]);
}
if(i==d&&!f[son[0][0]]) dp[o^1][son[0][0]][0]=dqm(dp[o^1][son[0][0]][0]-1);
if(i!=d) {
for(re int j=1;j<=9;j++) {
if(f[son[0][j]]) continue;
dp[o^1][son[0][j]][0]=qm(dp[o^1][son[0][j]][0]+1);
}
}
}
for(re int i=0;i<=cnt;i++) ans=dqm(ans-dp[o][i][0]),ans=dqm(ans-dp[o][i][1]);
return ans;
}
inline int chk(char *H) {
int nw=0;
for(re int i=1;i<=d;i++) {nw=son[nw][H[i]-'0'];if(f[nw]) return 1;}
return 0;
}
int main() {
scanf("%s",S+1);n=strlen(S+1);scanf("%s",A+1);scanf("%s",B+1);d=strlen(A+1);
for(re int i=1;i+(d>>1)-1<=n;i++) ins(i,i+(d>>1)-1);
std::queue<int> q;
for(re int i=0;i<=9;i++) if(son[0][i]) q.push(son[0][i]);
while(!q.empty()) {
int k=q.front();q.pop();f[k]|=f[fa[k]];
for(re int i=0;i<=9;i++)
if(son[k][i])fa[son[k][i]]=son[fa[k]][i],q.push(son[k][i]);else son[k][i]=son[fa[k]][i];
}
printf("%d\n",(solve(B)-solve(A)+mod+chk(A))%mod);
return 0;
}
CF585F Digits of Number Pi的更多相关文章
- 题解 CF585F 【Digits of Number Pi】
考虑用数位 \(DP\) 来统计数字串个数,用 \(SAM\) 来实现子串的匹配. 设状态 \(f(pos,cur,lenth,lim,flag)\),表示数位的位数,在 \(SAM\) 上的节点,匹 ...
- [CodeForces-585F]Digits of Number Pi
题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...
- 【leetcode】1295. Find Numbers with Even Number of Digits
题目如下: Given an array nums of integers, return how many of them contain an even number of digits. Exa ...
- 数学符号π (Pi)、Σ(Capital Sigma)、μ (Mu) 、σ(sigma)、∏(capital pi), ∫(Integral Symbol)的来历
1.π (Pi; periphery/周长) March 14 marks Pi Day, the holiday commemorating the mathematical constant π ...
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [Swift]LeetCode258. 各位相加 | Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- UVALive 4877 Non-Decreasing Digits 数位DP
4877 Non-Decreasing Digits A number is said to be made up ofnon-decreasing digitsif all the digits t ...
- POJ 1350 Cabric Number Problem (模拟)
题目链接 Description If we input a number formed by 4 digits and these digits are not all of one same va ...
随机推荐
- 组件化框架设计之阿里巴巴开源路由框架——ARouter原理分析(一)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 背景 当项目的业务越来越复杂,业务线越来越多的时候,就需要按照业 ...
- 牛客网挑战赛24 青蛙(BFS)
链接:https://www.nowcoder.com/acm/contest/157/E来源:牛客网 有一只可爱的老青蛙,在路的另一端发现了一个黑的东西,想过去一探究竟.于是便开始踏上了旅途 一直这 ...
- xampp环境下,配置Zend Studio调试php(XDebug) 转摘:http://www.cnblogs.com/tuyithief/archive/2011/06/02/2068431.html
先说一下文件版本,xampp 1.7.4,php 5.3.5. 走了很多弯路,截止目前,ZendDebugger在php 5.3.x下,只有nts版本,既non Thread Safety(具体什么意 ...
- Java Web servlet中的cookie
点击submit后: 点击查看Cookies: 在C:\Documents and Settings\Administrator\Cookies目录下面会有一个 hongten@webproj ...
- SAP Material Type on Classification Tree(ClassMaster management)
SAP Material Type on Classification Tree(ClassMaster management) 1. Classification Tree 是 luxottica ...
- js常用扩展方法
在日常的开发过程中,经常会碰到javaScript原生对象方法不够用的情况,所以经常会对javaScript原生方法进行扩展.下面就是在实际工作时,经常使用的一些方法,做一下记录,有需要的可以拿去. ...
- windows server 2012 R2修改默认远程端口
因客户现场网络复杂,将windows系统的默认远程端口3389归入安全策略中,所以服务器需要修改此端口,配置如下: 首先:登录操作系统,win+R调出运行菜单后输入regedit, 进入注册表编辑相关 ...
- 进程启动到别的session下(作用)
https://blog.csdn.net/lostwifi/article/details/76472868 WTSGetActiveConsoleSessionId WTSEnumerateSes ...
- npm启动报错
npm 启动报错 event.js 160 报错原因: 端口号被占用 解决方法: 1.重新定义一个端口号. 2.任务管理器关掉node进程,重新运行npm.
- java基础学习笔记二(接口、super、this)
一.super 和 this的用法 主要解释一下引用构造函数的用法 super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形式的构造函数(应 ...