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 ...
随机推荐
- RabbitMq--2--安装
简单说下个人的理解,mq就是一个消息代理,负责异步消息转发,可以很大程度缓解服务器压力,并且防止服务器宕机影响业务等. 安装: 环境:centos7 1).首先需要安装erlang #wget htt ...
- js实现倒计时+div下落
全部由js动态生成结点,body内无内容 <style> #count{ position: absolute; text-align: center; width: 240px; hei ...
- linux起源及centos安装
第1章 Linux介绍 1.1 什么是操作系统 是一个人与计算机硬件的中介 Linux:内核+shell+扩展软件 操作系统,英文名称Operating System,简称OS,是计算机系统中必不可 ...
- Python随笔记录之一
import os import random from copy import deepcopy ''' 读取特定目录下所有的文件夹, 和文件名 ''' def eachDir(path): for ...
- RIDE使用介绍
[转载] RIDE是一款专门用来编辑Robot Framework用例的软件,用Python编写并且开源.当我们针对一个系统编写好一套用例后,每当我们对系 统做一些更改的时候,便可以把已经写好的用例拿 ...
- 21.Semaphore信号量
Semaphore是一种基于计数的信号量.它可以设定一个阈值,基于此,多个线程竞争获取许可信号,做自己的申请后归还,超过阈值后,线程申请许可信号将会被阻塞.Semaphore可以用来构建一些对象池,资 ...
- tensorflow报错
libcublas.so.9.0: cannot open shared object file: No such file or directory 输入命令: sudo ldconfig/usr/ ...
- OpenCV常用基本处理函数(4)简单变换,阈值等
几何变换 缩放 img=cv2.imread('messi5.jpg') # 下面的 None 本应该是输出图像的尺寸,但是因为后边我们设置了缩放因子 # 因此这里为 None res=cv2.res ...
- Vue select 绑定动态变量
概述 根据后台的数据生成多个select,由于数据的数量不定,所以v-model绑定的变量名也不定.所以通过数据的id或者下标进行变量拼接.页面能够成功渲染,但是当进行下拉框的选值时,组件不刷新,选中 ...
- kubernetes部署metrics-server metrics-server-v0.3.4 pod报错
[root@hadoop02 ~]# kubectl logs metrics-server-v0.3.4-76db4dd54b-s4t2d -c metrics-server -n kube-sys ...