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 95 96 97 98 99 100 101
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const LL mod=1e9+7; const int maxn=5000005; int n,k; struct Trie { int root,cnt; int child[maxn][10]; int f[maxn],tmp1[21],tmp2[21]; LL Hash[maxn]; Trie() { cnt=0; root=++cnt; } int Get_Hash(int depth) { if(!depth)return 0; if(f[depth])return f[depth]; for(int i=0; i<=9; i++)f[depth]=((LL)f[depth]*131+i+Get_Hash(depth-1))%mod; return f[depth]; } void build(int Now,bool Up,bool Down,int pos) { if(pos==0)return; if(!Up&&!Down) { Hash[Now]=Get_Hash(pos); return; } int up=Up?tmp2[pos]:9,down=Down?tmp1[pos]:0; for(int i=down; i<=up; i++) { if(!child[Now][i])child[Now][i]=++cnt; build(child[Now][i],Up&&i==up,Down&&i==down,pos-1); } } void insert(LL Left,LL Right) { for(int i=1; i<=19; i++)tmp1[i]=Left%10,Left/=10; for(int i=1; i<=19; i++)tmp2[i]=Right%10,Right/=10; build(root,1,1,19); } void dfs(int Now) { for(int i=0; i<=9; i++) { int Next=child[Now][i]; if(!Next)continue; dfs(Next); Hash[Now]=((LL)Hash[Now]*131+Hash[Next]+i)%mod; } } bool check(int Now,int x,int y,int pos) { if(pos<=k&&((!child[Now][x])^(!child[Now][y])))return false; if(pos<=k&&child[Now][x]&&child[Now][y]&&Hash[child[Now][x]]!=Hash[child[Now][y]])return false; for(int i=0; i<=9; i++) if(child[Now][i]&&!check(child[Now][i],x,y,pos-1))return false; return true; } } trie; vector<int>ans[10]; int father[15]; int Get_Father(int x) { if(father[x]==x)return x; return father[x]=Get_Father(father[x]); } int main() { n=Get_Int(); k=Get_Int(); for(int i=1; i<=n; i++) { LL x=Get_Int(),y=Get_Int(); trie.insert(x,y); } trie.dfs(1); for(int i=1; i<=9; i++)father[i]=i; for(int i=1; i<=9; i++) for(int j=i+1; j<=9; j++) if(trie.check(1,i,j,19))father[Get_Father(j)]=Get_Father(i); for(int i=1; i<=9; i++)ans[Get_Father(i)].push_back(i); for(int i=1; i<=9; i++) { if(!ans[i].size())continue; for(int x:ans[i])printf("%d",x); putchar('\n'); } return 0; }
|