传送门

题意:

给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自动机)的更多相关文章

  1. 咕咕(数位dp+AC自动机)

    咕咕(数位dp+AC自动机) 若一个字符串的字符集合是0~m-1,那么称它为m进制字符串.给出n个m进制字符串\(s_i\),每个字符串的权值为\(v_i\).对于另一个m进制字符串\(S\),设\( ...

  2. CF 434C Tachibana Kanade's Tofu[数位dp+AC自动机]

    Solution //本代码压掉后两维 #include<cstdio> #define max(a,b) (a<b?b:a) using namespace std; inline ...

  3. [Sdoi2014]数数[数位dp+AC自动机]

    3530: [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 834  Solved: 434[Submit][Status][ ...

  4. shuoj 1 + 2 = 3? (二分+数位dp)

    题目传送门 1 + 2 = 3? 发布时间: 2018年4月15日 22:46   最后更新: 2018年4月15日 23:25   时间限制: 1000ms   内存限制: 128M 描述 埃森哲是 ...

  5. POJ3208 Apocalypse Someday(二分 数位DP)

    数位DP加二分 //数位dp,dfs记忆化搜索 #include<iostream> #include<cstdio> #include<cstring> usin ...

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

  7. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  8. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  9. Codeforces #55D-Beautiful numbers (数位dp)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. HTML/CSS基础知识(四)

    WEB标准和W3C的理解与认识 Web标准是一系列标准的集合. 网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior). 对应的标准也分三方面:结 ...

  2. MySQL——navicat 连接 mysql 出现1251Client does not support authentication protocol requested by server的解决方案

    前期修改root密码问题(首次安装的root密码是空,直接Enter就行): cmd用管理员身份进入,然后输入 mysqladmin -u root -p password newpassword 需 ...

  3. Spring Boot探究之旅--启动分析

    刚接触SpringBoot,感觉挺方便的,不用配置那么多乱七八糟的配置,很方便!酒饱思淫欲,得陇望蜀一下,看看SpringBoot到底怎么做到这么方便的. 首先呢,先来看个SpringBoot的hel ...

  4. tomcat advanced (RUNNING)

    1. 1. tomcat

  5. list.remove的使用分析

    场景描述 在做需求中,有很多情况会出现 对一个list遍历并过滤掉其中特定的数据 这种场景 .但是按照平常的使用方式,发现报错了. public static void main(String[] a ...

  6. rpm梳理

  7. SOA和微服务的原则及对比

    一.面向服务设计的原则 服务可复用:不管是否存在即时复用的机会,服务均被设计为支持潜在的可复用 服务共享一个标准契约:为了与服务提供者交互,消费者需要导入服务提供者的服务契约,这个契约可以是一个IDL ...

  8. Apache配置默认主页

    Apache配置默认主页 进入Apache的conf目录 打开httpd.conf文件输入: 在文件末位添加: <Directory "F:/www_php/blog_com/my_b ...

  9. python中安装request模块

    使用命令行(cmd)进入到python安装目录下的Scripts文件夹(也可以先进入目标文件夹然后在文件路径上打开cmd回车即可) 在命令行中输入pip install requests,等待安装完成 ...

  10. 吴裕雄 python深度学习与实践(8)

    import cv2 import numpy as np img = cv2.imread("G:\\MyLearning\\TensorFlow_deep_learn\\data\\le ...