考虑用数位 \(DP\) 来统计数字串个数,用 \(SAM\) 来实现子串的匹配。

设状态 \(f(pos,cur,lenth,lim,flag)\),表示数位的位数,在 \(SAM\) 上的节点,匹配的长度,是否有最高位限制,是否已经满足要求。

在 \(dfs\) 转移时,若当前节点能接着匹配枚举到的字符,就直接转移,若不能,则在 \(Parent\) 树上向上跳,直到能接着匹配,转移过程中判断是否满足条件即可。

\(code:\)

#include<bits/stdc++.h>
#define maxn 2010
#define maxd 55
#define mod 1000000007
using namespace std;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
int n,d,tot=1,las=1,root=1,cnt;
int fa[maxn],len[maxn],ch[maxn][12],num[maxn],f[maxd][maxn][maxd][2][2];
char s[maxn],a[maxn],b[maxn];
void insert(int c)
{
int p=las,np=las=++tot;
len[np]=len[p]+1;
while(p&&!ch[p][c]) ch[p][c]=np,p=fa[p];
if(!p) fa[np]=root;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1) fa[np]=q;
else
{
int nq=++tot;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
len[nq]=len[p]+1,fa[nq]=fa[q],fa[np]=fa[q]=nq;
while(ch[p][c]==q) ch[p][c]=nq,p=fa[p];
}
}
}
int dp(int pos,int cur,int lenth,bool lim,bool flag)
{
if(pos==cnt+1) return flag;
if(f[pos][cur][lenth][lim][flag]!=-1) return f[pos][cur][lenth][lim][flag];
int v=0,ma=9;
if(lim) ma=num[pos];
for(int c=0;c<=ma;++c)
{
if(flag) v=(v+dp(pos+1,cur,lenth,lim&&c==ma,1))%mod;
else
{
int p=cur;
if(ch[p][c]) v=(v+dp(pos+1,ch[p][c],lenth+1,lim&&c==ma,lenth+1>=d/2))%mod;
else
{
while(p&&!ch[p][c]) p=fa[p];
if(p) v=(v+dp(pos+1,ch[p][c],len[p]+1,lim&&c==ma,len[p]+1>=d/2))%mod;
else v=(v+dp(pos+1,root,0,lim&&c==ma,0))%mod;
}
}
}
return f[pos][cur][lenth][lim][flag]=v;
}
int solve(char *s)
{
cnt=strlen(s+1),memset(f,-1,sizeof(f));
for(int i=1;i<=cnt;++i) num[i]=s[i]-'0';
return dp(1,root,0,1,0);
}
int main()
{
scanf("%s%s%s",s+1,a+1,b+1),n=strlen(s+1),d=cnt=strlen(a+1),a[cnt--]--;
for(int i=1;i<=n;++i) insert(s[i]-'0');
while(a[cnt]-'0'==-1) a[cnt]--,a[cnt--]=9+'0';
printf("%d",(solve(b)-solve(a)+mod)%mod);
return 0;
}

题解 CF585F 【Digits of Number Pi】的更多相关文章

  1. CF585F Digits of Number Pi

    题目 把\(s\)串所有长度为\(\lfloor \frac{d}{2}\rfloor\)的子串插入一个ACAM中,之后数位dp就好了,状态是\(dp_{i,j,0/1}\)第\(i\)位,在ACAM ...

  2. [CodeForces-585F]Digits of Number Pi

    题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...

  3. 题解-Ehab's REAL Number Theory Problem

    Ehab's REAL Number Theory Problem 前置知识 质数 分解质因数 无向无权图最小环<讲> Ehab's REAL Number Theory Problem/ ...

  4. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

  5. PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚

    n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...

  6. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  7. 【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 ...

  8. ICPC — International Collegiate Programming Contest Asia Regional Contest, Yokohama, 2018–12–09 题解

    目录 注意!!此题解存在大量假算法,请各位巨佬明辨! Problem A Digits Are Not Just Characters 题面 题意 思路 代码 Problem B Arithmetic ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端

    1 该项目的主要功能是:后台通过xml或者json格式返回后台的视频资讯,然后Android客户端界面显示出来 首先后台新建立一个java web后台 采用mvc的框架 所以的servlet都放在se ...

  2. vueX基础知识点笔记

    vuex是专门用来管理vue.js应用程序中状态的一个插件.他的作用是将应用中的所有状态都放在一起, 集中式来管理.需要声明的是,这里所说的状态指的是vue组件中data里面的属性.简单的来说, 它就 ...

  3. Python实用笔记 (23)面向对象高级编程——使用__slots__

    正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Student(object): pa ...

  4. Python实用笔记 (11)高级特性——迭代器

    这些可以直接作用于for循环的对象统称为可迭代对象:Iterable. 可以使用isinstance()判断一个对象是否是Iterable对象: >>> from collectio ...

  5. 字节流,读取 a.txt 文件内容,并打印出来

    import java.io.FileInputStream;import java.io.IOException; /** 字节流,读取 a.txt 文件内容,并打印出来 */public clas ...

  6. 《JavaScript高级程序设计》(第二版)

    这本书的作者是 Nicholas C.Zakas ,博客地址是 http://www.nczonline.net/ ,大家可以去多关注,雅虎的前端工程师,是YUI的代码贡献者,可想而知这本书得含金量, ...

  7. Material Component--mdcChipSet使用

    <div mdcChipSet="choice"> <div mdcChip *ngFor="let item of ywDicTypes" ...

  8. 理解并使用CSS3中的单位rem vh vw vmin vmax

    rem vh vw vmin vmax做为CSS3中的新单位,其实都出来挺久的了,这篇文章将总结并理解下它们. rem 如果你给body设置了font-size字体大小,那么body的任何子元素的1e ...

  9. windows php5.5安装redis扩展,并用redis存储session

    1.确定安装版本 先通过phpinfo()查看php的Compiler.Architecture.Thread Safety,其中Thread Safety如果是enabled,那么就是线程安全(ts ...

  10. 使用@AutoConfigureBefore调整配置顺序竟没生效?

    一个人的价值体现在能够帮助多少人.自己编码好,价值能得到很好的体现.若你做出来的东西能够帮助别人开发,大大减少开发的时间,那就功德无量. 作者:A哥(YourBatman) 公众号:BAT的乌托邦(I ...