All submissions for this problem are available.

Chef likes numbers and number theory, we all know that. There are N digit strings that he particularly likes. He likes them so much that he defines some numbers to be beautiful numbers based on these digit strings.

Beautiful numbers are those numbers whose decimal representation contains at least one of chef's favorite digit strings as a substring. Your task is to calculate the Kth smallest number amongst the beautiful numbers in the range from L to R (both inclusive). If the number of beautiful numbers between L and R is less than K, then output "no such number".

Input

In the first line of input there will be integers L, R, K and N. Then N lines follow. Each line will contain a single string of decimal digits.

Output

Output one integer - the solution to the problem described above or a string "no such number" if there is no such number.

Constraints

  • 1<=L<=R<=10^18
  • 1<=K<=R-L+1
  • 1<=N<=62
  • 1<=The length of any Chef's favourite digit string<=18. Each string begins with a nonzero digit.

Example

Input:
1 1000000000 4 2
62
63 Output:
163
Input:
1 1 1 1
2 Output:
no such number
Input:
1 1000 15 2
6
22 Output:
67

Author:6★xcwgf666

Tester:6★laycurse

Editorial:http://discuss.codechef.com/problems/FAVNUM

Tags:aho-corasickdynamic-programmingjuly12mediumxcwgf666

Date Added:20-12-2011

Time Limit:0.1 - 0.146779 secs

Source Limit:50000 Bytes

Languages:C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS

题解:给你一些幸运数字,然后问你在一个区间内的第k大的,含有幸运数字的数是哪一个数字。

第K大,二分答案,然后考虑数位DP。如果是单个数字的话这样就可以了。多个数字,我们把它们放在AC自动机里面,然后dp[len][pos][flag]表示当前长度为len,走到AC自动机上pos点的时候取幸运数字状态为flag(是或否取到)的方案数。那么我们显然可以有转移方程:dp[len][pos][flag]=Σ dp[len-1][ch][flag || AC.T[ch].cnt],其中ch为pos的某一个后继节点。意思是,如果到下一个节点能够组成一个幸运数字,那么flag的状态就要相应改变。之后,按照普通数位dp的套路那样,记忆化搜索转移即可。

 

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 2400
int n,tot;
ll L,R,K;
char s[];
int ch[maxn][],fail[maxn],val[maxn],last[maxn]; void Init()
{
tot=;
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
}
int idx(char c){ return c - '';}
void Insert(char*s)
{
int u=,len=strlen(s);
for(int i=;i<len;++i)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[tot],,sizeof(ch[tot]));
val[tot]=;
ch[u][c]=tot++;
}
u=ch[u][c];
}
val[u]=;
}
void GetFail()
{
queue<int> q;
fail[]=;
for(int c=;c<;++c)
{
int u=ch[][c];
if(u){ fail[u]=;q.push(u);last[u]=; }
}
while(!q.empty())
{
int r=q.front(); q.pop();
val[r]|=val[fail[r]];
for(int c=;c<;++c)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
int v=fail[r];
fail[u]=ch[v][c];
last[u] = val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
int dig[];
ll dp[][maxn][];
ll dfs(int len,int pos,bool flag,int lim)
{
if(len<=) return flag;
if(!lim&&dp[len][pos][flag]>=) return dp[len][pos][flag];
ll res=;
int sz=lim?dig[len]:;
for(int i=;i<=sz;++i)
{
int nxt=ch[pos][i];
res+=dfs(len-,nxt,val[nxt]||flag,lim&&i==sz);
}
if(!lim) dp[len][pos][flag]=res;
return res;
}
ll work(ll x)
{
int len=;
while(x)
{
dig[++len]=x%;
x/=;
}
return dfs(len,,,);
} int main()
{
scanf("%lld%lld%lld%d",&L,&R,&K,&n);
Init();
for(int i=;i<=n;++i)
{
scanf("%s",s);
Insert(s);
}
GetFail();
memset(dp,-,sizeof dp);
ll num=work(L-),mid,ans=;
while(L<=R)
{
mid=L+R>>;
if(work(mid)-num>=K) R=mid-,ans=mid;
else L=mid+;
}
if(ans) printf("%lld\n",ans);
else puts("no such number"); return ;
}

CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)的更多相关文章

  1. 【HDU3530】 [Sdoi2014]数数 (AC自动机+数位DP)

    3530: [Sdoi2014]数数 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 682  Solved: 364 Description 我们称一 ...

  2. 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp

    题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...

  3. BZOJ 3530 [SDOI2014]数数 (Trie图/AC自动机+数位DP)

    题目大意:略 裸的AC自动机+数位DP吧... 定义f[i][x][0/1]表示已经匹配到了第i位,当前位置是x,0表示没到上限,1到上限,此时数是数量 然而会出现虚拟前导零,即前几位没有数字的情况, ...

  4. HDU-4518 吉哥系列故事——最终数 AC自动机+数位DP

    题意:如果一个数中的某一段是长度大于2的菲波那契数,那么这个数就被定义为F数,前几个F数是13,21,34,55......将这些数字进行编号,a1 = 13, a2 = 21.现给定一个数n,输出和 ...

  5. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...

  6. zoj3494BCD Code(ac自动机+数位dp)

    l链接 这题想了好一会呢..刚开始想错了,以为用自动机预处理出k长度可以包含的合法的数的个数,然后再数位dp一下就行了,写到一半发现不对,还要处理当前走的时候是不是为合法的,这一点无法移到trie树上 ...

  7. BZOJ3530:[SDOI2014]数数(AC自动机,数位DP)

    Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...

  8. BCD Code ZOJ - 3494 AC自动机+数位DP

    题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...

  9. 【JZOJ3624】【SDOI2014】数数(count) AC自动机+数位dp

    题面 100 容易想到使用AC自动机来处理禁忌子串的问题: 然后在自动机上数位dp,具体是: \(f_{i,j,0/1}\)表示填了\(i\)位,当前在自动机的第\(j\)个结点上,\(0\)表示当前 ...

随机推荐

  1. TRANK和VTP

    需求:因为公司规模逐渐扩大,出现相同部门不同办公室的情况,老板提出新的要求:相同部门可以通信,不同部门不能通信. 利用vlan: 缺点:浪费材料,应用技术手段把两条交叉线变成一条. 因此,引进trun ...

  2. haproxy+keepalived练习

    小的网站结构 说明:如果部署在云上,比如阿里云上,不需要自己部署keepalived,直接买阿里云的slb即可,slb然后分发流量到两台haproxy机器 一.先部署两个web服务器 编译安装ngin ...

  3. [TCP/IP] 学习TCP/IP协议的笔记

    1.我看的视频是https://www.bilibili.com/video/av10610680?from=search&seid=1733008388243131444这位大大的视频讲解. ...

  4. 使用 MUI 自制 弹出层

    使用 MUI 自制 弹出层 <div class="zp-mask" style="display: none; width: 100%;height: 100%; ...

  5. requests模拟登陆的三种方式

    ###获取登录后的页面三种方式: 一.实例化seesion,使用seesion发送post请求,在使用他获取登陆后的页面 import requests session = requests.sess ...

  6. windows:查看电脑开放的端口

    netstat -ano netstat -ano | findstr '445' 查看445端口是否被使用 根据端口找到占用程序的PID,再用tasklist|findstr "2720& ...

  7. Mac 下安装并配置 Tomcat

    1,下载 点击 官网 ,进入下载页面, 2,安装 解压出来,即安装完成. 移动解压后的文件,换个文件目录(方便集中管理),将它改个名字(毕竟名字太长了). 我将其改名为 tomcat9 ,移入资源库目 ...

  8. vscode在终端运行脚本时出现“因为在此系统上禁止运行脚本”

    首先关闭vscode,再以管理员的身份运行vscode,然后打开终端执行: get-ExecutionPolicy,显示的是Restricted,表示状态是禁止的; 再执行:set-Execution ...

  9. 【译】为什么永远都不要使用MongoDB Why You Should Never Use MongoDB

    背景 最近在学习DDIA(Designing Data-Intensive Applications)这本分布式领域非常急经典的入门书籍,里面第二章<数据模型与查询语言>,强调了对一对多. ...

  10. python+selenium +unittest生成HTML测试报告

    python+selenium+HTMLTestRunner+unittest生成HTML测试报告 首先要准备HTMLTestRunner文件,官网的HTMLTestRunner是python2语法写 ...