传送门

题意:

给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. spring mvc 注解整理(一)

    @Controller和@RestController: RestController = @ResponseBody + @Controller  所有返回都是json类型,无法跳转到jsp页面,但 ...

  2. μC/Probe尝鲜

    μC/Probe 1.添加文件 2.配置probe_com_cfg.h 2.1.选择接口 #define PROBE_COM_CFG_RS232_EN DEF_ENABLED /* Configure ...

  3. 白话RPC

    RPC,这个英文缩写在计算机专业里的意思是:Remote Procedure Call Protocol,远程过程调用协议,字面上的意思就是这个,不过还是有些懵逼. 下面就简单说明一下其内在原理,形象 ...

  4. Python实践练习:强口令检测

    题目: 写一个函数,它使用正则表达式,确保传入的口令字符串是强口令.强口令的定义是:长度不少于 8 个字符,同时包含大写和小写字符,至少有一位数字.你可能需要用多个正则表达式来测试该字符串,以保证它的 ...

  5. leetcode62

    使用排列组合计算公式来计算,注意使用long long型数据保证计算不会溢出. class Solution { public: int M, N; ; //从根到叶子有多少个分支,就表示有多少种路径 ...

  6. xml实现登录表单验证

    定义: XML(eXtended Markup Language,可扩展标记语言)提供了一套跨平台.跨网络.跨程序的语言的数据描述方式,使用XML可以方便地实现数据交换.系统配置.内容管理等常见功能. ...

  7. The history of programming languages.(transshipment) + Personal understanding and prediction

    To finish this week's homework that introduce the history of programming languages , I surf the inte ...

  8. js中遇到的一些方法和函数

    这是一个笔记文章,方便日后复习. 加号的优先级高于三目运算符: console.log(') ? 'define' : 'undefine');//define setTimeout(code,mil ...

  9. 【c++】内存检查工具Valgrind介绍,安装及使用以及内存泄漏的常见原因

    转自:https://www.cnblogs.com/LyndonYoung/articles/5320277.html Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,它包 ...

  10. 【开发工具】secureCRT的使用

    总结自己平时使用secureCRT中遇到的问题 1.安装 2.secureCRT设置标签显示远程主机ip地址