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. ES6学习笔记01 -- 暂时性死区 ( temporal dead zone )

    参考文档: let 和 const 命令 - ECMAScript6入门  暂时性死区(temporal dead zone) 理解ES6中的TDZ(暂时性死区) ES6 中 let 暂时性死区详解 ...

  2. Algorithm: GCD、EXGCD、Inverse Element

    数论基础 数论是纯数学的一个研究分支,主要研究整数的性质.初等数论包括整除理论.同余理论.连分数理论.这一篇主要记录的是同余相关的基础知识. 取模 取模是一种运算,本质就是带余除法,运算结果就是余数. ...

  3. thinkphp 6.0 在 initialize 中重定向无效

    thinkphp 6.0 在 initialize 中重定向无效 改用 header() 函数 实例: // header('location:/index.php/模块/控制器/方法'); head ...

  4. lqb 基础练习 数列排序 (sort的使用)

    基础练习 数列排序 时间限制:1.0s   内存限制:512.0MB     问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列.1<=n<=200 输入格式 第一行为一个整 ...

  5. 1005 Spell It Right(20 分)

    1005 Spell It Right(20 分) Given a non-negative integer N, your task is to compute the sum of all the ...

  6. MySQL/MariaDB读写分离配置

    DB读写分离描述 数据库的读写分离其实就是为了加减少数据库的压力:数据库的写入操作由主数据库来进行,读取操作由从数据库来进行操作.实现数据库读写分离技术是有很多方法的,在这里我就用一个比较简单的mys ...

  7. windows 10 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on windows 10

    本文首发于个人博客https://kezunlin.me/post/8b9c051d/,欢迎阅读! Interfacing C++ and Python with pybind11 on window ...

  8. Redshitf Install

    创建VPC 和 子网和internet网关(子网需开启自动分配公网IP,VPN 需添加到internet网关的路由) 创建安全组: 创建cluster subnet Group; 创建redshift ...

  9. 新闻实时分析系统 Spark2.X分布式弹性数据集

    1.三大弹性数据集介绍 1)概念 2)优缺点对比 2.Spark RDD概述与创建方式 1)概述 在集群背后,有一个非常重要的分布式数据架构,即弹性分布式数据集(resilientdistribute ...

  10. JVM系列一(Java内存区域和对象创建).

    一.JVM 内存区域 堆 - Heap 线程共享,JVM中最大的一块内存,此内存的唯一目的就是存放对象实例,Java 堆是垃圾收集器管理的主要区域,因此很多时候也被称为"GC堆"( ...