2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)
传送门
题意:
给444个整数L,R,K,nL,R,K,nL,R,K,n,和nnn个数字串,L,R,K,数字串大小≤1e18,n≤65L,R,K,数字串大小\le1e18,n\le65L,R,K,数字串大小≤1e18,n≤65
问[L,R][L,R][L,R]中第KKK小的拥有nnn个数字串中至少一个串作为子串的数。
思路:
一看就要二分答案,现在考虑统计[L,R][L,R][L,R]中有多少个满足题意的数。
不妨考虑数位dpdpdp,然后发现没法很好的转移,为了优化转移可以对于所有的数字串构建一个acacac自动机来优化转移。
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
const int N=2005;
typedef long long ll;
namespace acam{
int ch[N][10],fail[N],tot=0,q[N],hd,tl;
bool exi[N];
#define idx(x) ((x)^48)
inline void init(){memset(ch[0],0,sizeof(ch[0])),memset(fail,0,sizeof(fail)),exi[0]=0,tot=0;}
inline int build(){return exi[++tot]=0,memset(ch[tot],0,sizeof(ch[tot])),tot;}
inline void insert(char s[]){
int p=0,n=strlen(s);
for(ri x,i=0;i<n;++i){
if(!ch[p][(x=idx(s[i]))])ch[p][x]=build();
p=ch[p][x];
}
exi[p]=1;
}
inline void getfail(){
hd=1,tl=0;
for(ri i=0;i<10;++i)if(ch[0][i])q[++tl]=ch[0][i];
while(hd<=tl){
int p=q[hd++];
for(ri i=0,v;i<10;++i){
if((v=ch[p][i]))fail[v]=ch[fail[p]][i],q[++tl]=v,exi[v]|=exi[fail[v]];
else ch[p][i]=ch[fail[p]][i];
}
}
}
#undef idx
}
namespace dDP{
ll f[20][N][2];
vector<int>Up,Down;
inline vector<int>init(ll x){
vector<int>ret;
ret.clear();
if(!x)return ret.push_back(x),ret;
while(x)ret.push_back(x-x/10*10),x/=10;
return ret;
}
inline ll dfs(int pos,int sta,bool flag,bool down,bool up,bool zero){
if(pos==-1)return flag;
if(!up&&!down&&!zero&&~f[pos][sta][flag])return f[pos][sta][flag];
ll ret=0;
for(ri l=down?Down[pos]:0,r=up?Up[pos]:9,i=l;i<=r;++i){
if(zero&&!i&&pos)ret+=dfs(pos-1,0,flag,down&&i==l,up&&i==r,1);
else ret+=dfs(pos-1,acam::ch[sta][i],flag|acam::exi[acam::ch[sta][i]],down&&i==l,up&&i==r,zero&&!i);
}
if(!up&&!down&&!zero)f[pos][sta][flag]=ret;
return ret;
}
inline ll solve(ll l,ll r){
int len1,len2;
Down=init(l),len1=Down.size();
Up=init(r),len2=Up.size();
for(ri i=len1+1;i<=len2;++i)Down.push_back(0);
return dfs(Up.size()-1,0,0,1,1,1);
}
}
char s[100];
int main(){
ll a,b,K,l,r,ans;
int n;
cin>>a>>b>>K>>n;
acam::init();
while(n--)scanf("%s",s),acam::insert(s);
acam::getfail();
l=a,r=b+1,ans=b+1;
memset(dDP::f,-1,sizeof(dDP::f));
while(l<=r){
ll mid=l+r>>1;
if(dDP::solve(a,mid)<K)l=mid+1;
else r=mid-1,ans=mid;
}
if(ans==b+1)puts("no such number");
else cout<<ans;
return 0;
}
2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)的更多相关文章
- 咕咕(数位dp+AC自动机)
咕咕(数位dp+AC自动机) 若一个字符串的字符集合是0~m-1,那么称它为m进制字符串.给出n个m进制字符串\(s_i\),每个字符串的权值为\(v_i\).对于另一个m进制字符串\(S\),设\( ...
- CF 434C Tachibana Kanade's Tofu[数位dp+AC自动机]
Solution //本代码压掉后两维 #include<cstdio> #define max(a,b) (a<b?b:a) using namespace std; inline ...
- [Sdoi2014]数数[数位dp+AC自动机]
3530: [Sdoi2014]数数 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 834 Solved: 434[Submit][Status][ ...
- shuoj 1 + 2 = 3? (二分+数位dp)
题目传送门 1 + 2 = 3? 发布时间: 2018年4月15日 22:46 最后更新: 2018年4月15日 23:25 时间限制: 1000ms 内存限制: 128M 描述 埃森哲是 ...
- POJ3208 Apocalypse Someday(二分 数位DP)
数位DP加二分 //数位dp,dfs记忆化搜索 #include<iostream> #include<cstdio> #include<cstring> usin ...
- Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Balanced Numbers (数位dp+三进制)
SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...
- Codeforces #55D-Beautiful numbers (数位dp)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- Nginx、HAProxy、LVS三者的优缺点
一.Nginx优点: 1.工作在网络7层之上,可针对http应用做一些分流的策略,如针对域名.目录结构,它的正规规则比HAProxy更为强大和灵活,所以,目前为止广泛流行. 2.Nginx对网络稳定性 ...
- 学习 MeteoInfo二次开发教程(四)
教程四的问题不大. 1.private void AddMapFrame_ChinaSouthSea().private void AddTitle()两个函数和public Form1()函数并列. ...
- 一、CSS介绍
CSS介绍 1.css概述: CSS指层叠样式表 CSS样式表极大地提高了工作效率 selector{ property:value } 注:property(属性)大于1之后,property之间用 ...
- Eclipse关于怎么调出web project
myeclipse和eclipse两个软件不一样的点很多,当然玩的时候也会遇到找不到的选项 此片摘自: https://www.cnblogs.com/icebutterfly/p/7771936.h ...
- mapper.xml文件,sql语句参数为list
<insert id="insertPjCustomAttribute" parameterType="com.devops.server.model.PjCust ...
- Generalizations
Generalizations Congratulations! You've learned five commands commonly used to navigate the filesyst ...
- test case VS test scenario
---恢复内容开始--- 1. test case: how to test --如何测试 test scenario: what to be tested --测试什么 2. test scen ...
- (转)VS2010反编译dll之后存在的resource修改为resx
https://www.cnblogs.com/tangbaono1/p/6897183.html 1.找到安装VS的时候,存在的ResGen.exe,我的电脑是win7的,路径是在C:\Progra ...
- R语言-地图
1.maps包的map()函数 >map('world', fill = TRUE,col=heat.colors(10)) #世界地图 >map("state", i ...
- psdTohtml
https://github.com/anjorweb/fastHtml fastHtml 一个简单的psd直接导出html的工具 自己工作常用整理 适合单页面且采用DOM结构布局的H5页面,基于Ca ...