1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std;
typedef long long LL;
const int maxtot=1e6+5, maxnp=30, maxn=1e5+5; const LL mo=1e9+7, px=998244353;
int n,m,k,np,a[15]; LL mm,powpx[maxnp];
unordered_map<LL,int> M; int tot; vector<pair<int,int>> e[maxtot]; void dfs1(int k,LL now) { if (k==np) { M[now]=++tot; return; } fo(i,1,m) dfs1(k+1,(now*px+a[i])%mo); } void dfs2(int k,LL now,int fir) { if (k==np) { int my=M[now]; fo(i,1,m) { int go=M[((now*px+a[i]-fir*powpx[np-1])%mo+mo)%mo]; e[my].push_back(make_pair(go,a[i])); } return; } fo(i,1,m) dfs2(k+1,(now*px+a[i])%mo,(fir==-1)?a[i]:fir); }
int nowh[maxn],d0,st,d[maxn]; void Euler(int k,int lim) { int sz=e[k].size(); for(int i=nowh[k]; i<sz; i=nowh[k]) { nowh[k]=i+1; Euler(e[k][i].first,lim); if (d0==lim) return; d[++d0]=e[k][i].second; st=k; if (d0==lim) return; } }
int rc[maxn]; bool pd; void dfs3(int k,LL now) { if (k==np) { if (M[now]==st) { fo(i,1,np-1) printf("%d",rc[i]); pd=1; } return; } fo(i,1,m) { rc[k]=a[i]; dfs3(k+1,(now*px+a[i])%mo); if (pd) return; } }
int main() { scanf("%d %d %d",&n,&m,&k); fo(i,1,m) scanf("%d",&a[i]); for(np=1, mm=m; mm<k; np++, mm*=m); powpx[0]=1; fo(i,1,np) powpx[i]=powpx[i-1]*px%mo; dfs1(1,0); dfs2(1,0,-1); Euler(1,k); fo(i,1,n-np) printf("%d",a[1]); dfs3(1,0); fd(i,d0,1) printf("%d",d[i]); }
|