#include<bits/stdc++.h>
using namespace std;
using ll=long long int;
using llu=long long unsigned int;
const int N=1e6+5;
//int x;
int g[N][26];
queue<int> q;
int states=1;
int f[N];
int isEnd[N];
string str;
string s;
llu dp[2500][35][2][2][2];
int visited[2500][35][2][2][2];
int n;
int nextstate(int &state,int ch){
while(g[state][ch]==-1)
state=f[state];
state=g[state][ch];
return isEnd[state]?1:0;
}
llu fun(int state,int indx,int flag1,int flag2,int isput){
if(indx==n){
if(flag2==1 && isput==1)return 1;
else
return 0;
}
llu ans=0;
if(visited[state][indx][flag1][flag2][isput]!=-1)
return dp[state][indx][flag1][flag2][isput];
int max=9;
if(flag1==0)max=s[indx]-'0';
int min=0;
if(flag2==1){
for(int i=min;i<=max;i++){
if(i!=max)
ans+=fun(state,indx+1,1,1,isput|(i>0));
else
ans+=fun(state,indx+1,flag1,1,isput|(i>0));
}
}else{
for(int i=min;i<=max;i++){
int cs=state;
int state1=nextstate(cs,i);
if(i!=max)
ans+=fun(cs,indx+1,1,flag2|state1,isput|(i>0));
else
ans+=fun(cs,indx+1,flag1,flag2|state1,isput|(i>0));
}
}
visited[state][indx][flag1][flag2][isput]=1;
return dp[state][indx][flag1][flag2][isput]=ans;
}
llu fun2222(llu x){
s="";
memset(visited,-1,sizeof(visited));
memset(dp,0,sizeof(dp));
while(x){
int d=x%10;
x=x/10LL;
s=to_string(d)+s;
}
n=s.length();
llu ans=fun(0,0,0,0,0);
return ans;
}
void add(int num){
int len=str.length();
int temp=0;
for(int i=0;i<len;i++){
int ch=str[i]-'0';
if(g[temp][ch]!=-1)
temp=g[temp][ch];
else
temp=g[temp][ch]=states++;
}
isEnd[temp]=num;
}
void make(){
memset(f,-1,sizeof(f));
for(int i=0;i<=9;i++){
if(g[0][i]!=-1)
{
f[g[0][i]]=0;
q.push(g[0][i]);
}
}
for(int i=0;i<=9;i++){
if(g[0][i]==-1){
g[0][i]=0;
}
}
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<=9;i++){
if(g[x][i]!=-1){
int failure=f[x];
while(g[failure][i]==-1)
failure=f[failure];
f[g[x][i]]=g[failure][i];
q.push(g[x][i]);
}
}
}
}
int main() {
for (int it = 0; it < 1000; it++) {
llu l, r, k;
int n;
states = 1;
memset(isEnd, 0, sizeof(isEnd));
// cin >> l >> r >> k >> n;
l = rand() + 1, r = rand() + l, k = rand() % (r - l) + 1;
n = rand() % 60 + 1;
memset(g, -1, sizeof(g));
vector<string> in;
for (int i = 1; i <= n; i++) {
// cin >> str;
int len = rand() % 18 + 1;
str = "";
for (int i = 0; i < len; i++) {
str += (i == 0 ? rand() % 9 + 1 : rand() % 10) + '0';
}
in.push_back(str);
add(i);
}
make();
llu low = l;
llu high = r;
llu ans22 = 0;
while (low <= high) {
llu mid = (low + high) / 2LL;
llu ans = fun2222(mid) - fun2222(l - 1);
if (ans >= k) {
ans22 = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
llu corans = 0;
for (llu a = l, cnt = 0; a <= r; a++) {
string curs = to_string(a);
for (int i = 0; i < n; i++) {
if (curs.find(in[i]) != curs.npos) {
cnt++;
break;
}
}
if (cnt == k) {
corans = a;
break;
}
}
if (ans22 != corans) {
cout << l << ' ' << r << ' ' << k << ' ' << n << '\n';
for (int i = 0; i < n; i++)
cout << in[i] << '\n';
cout << ans22 << ' ' << corans << '\n';
cout << fun2222(corans) << ' ' << fun2222(ans22) << ' '
<< fun2222(l - 1) << '\n';
return -1;
}
if (ans22 == 0) {
cout << "no such number" << endl;
} else {
cout << ans22 << endl;
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGw9bG9uZyBsb25nIGludDsKdXNpbmcgbGx1PWxvbmcgbG9uZyB1bnNpZ25lZCBpbnQ7CmNvbnN0IGludCBOPTFlNis1OwoKLy9pbnQgeDsKaW50IGdbTl1bMjZdOwpxdWV1ZTxpbnQ+IHE7CmludCBzdGF0ZXM9MTsKaW50IGZb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