Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- using namespace std;
- const int N = 1000;
- int k, m;
- bool * d[5][N];
- int i, j, u, v, e, x, qa, ne, s, y, top, as[10];
- vector<int> g[N];
- int ma[10];
- void PR(int ar)
- {
- int i;
- for(i = 1; i <= 8; ++ i)
- {
- ma[i] = 0;
- }
- i = 0;
- while(ar)
- {
- ++ i;
- ma[i] = ar%10;
- ar = ar/10;
- }
- for(i = 8; i > 0; -- i)
- {
- printf("%d", ma[i]);
- }
- printf("\n");
- }
- int main()
- {
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- scanf("%d%d", &k, &m);
- as[0] = 1;
- for(i = 1; i <= 6; ++ i)
- {
- as[i] = as[i-1]*10;
- }
- for(e = 1; e <= 3; ++ e)
- {
- d[e][0] = new bool [as[e]];
- d[e][0][0] = 1;
- for(i = 1; i < as[e]; ++ i)
- {
- d[e][i] = new bool [ as[e] ];
- if(i%10==0)
- {
- d[e][i][0] = 1;
- }
- x = i;s = 0;qa = 0;
- while(x)
- {
- s = s+as[qa]*(x%10);
- x = x/10;
- ++qa;
- ne = e-qa;
- if(ne > 0)
- {
- for(j = 0; j < as[e]; ++ j)
- {
- if(!d[e][i][j])
- {
- y = j-s;
- if(y >= 0 && y < as[ne] && d[ne][x][y])
- {
- d[e][i][j] = 1;
- continue;
- }
- y = j+s;
- if(y >= 0 && y < as[ne] && d[ne][x][y])
- {
- d[e][i][j] = 1;
- continue;
- }
- if(s!=0 && j%s==0)
- {
- y = j/s;
- if(y < as[ne] && d[ne][x][y])
- {
- d[e][i][j] = 1;
- }
- }
- }
- }
- }
- else
- {
- d[e][s][s] = 1;
- }
- }
- }
- }
- for(i = 0; i < N; ++ i)
- {
- for(j = 0; j < N; ++ j)
- {
- if(d[3][i][j])
- {
- g[j].push_back(i);
- }
- d[3][i][j] = 0;
- }
- }
- for(i = 0; i < N; ++ i)
- {
- j = k-i;
- if(j >= 0 && j < N)
- {
- for(u = 0; u < g[i].size(); ++ u)
- {
- for(v = 0; v < g[j].size(); ++ v)
- {
- if(d[3][ g[i][u] ][ g[j][v] ]==0)
- {
- ++ top;
- PR(g[i][u]*as[4]+g[j][v]);
- if(top==m)
- goto x;
- d[3][ g[i][u] ][ g[j][v] ] = 1;
- }
- }
- }
- }
- j = i-k;
- if(j >= 0 && j < N)
- {
- for(u = 0; u < g[i].size(); ++ u)
- {
- for(v = 0; v < g[j].size(); ++ v)
- {
- if(d[3][ g[i][u] ][ g[j][v] ]==0)
- {
- ++ top;
- PR(g[i][u]*as[4]+g[j][v]);
- if(top==m)
- goto x;
- d[3][ g[i][u] ][ g[j][v] ] = 1;
- }
- }
- }
- }
- j = i+k;
- if(j >= 0 && j < N)
- {
- for(u = 0; u < g[i].size(); ++ u)
- {
- for(v = 0; v < g[j].size(); ++ v)
- {
- if(d[3][ g[i][u] ][ g[j][v] ]==0)
- {
- ++ top;
- PR(g[i][u]*as[4]+g[j][v]);
- if(top==m)
- goto x;
- d[3][ g[i][u] ][ g[j][v] ] = 1;
- }
- }
- }
- }
- if(i!=0 && k%i==0)
- {
- j = k/i;
- if(j >= 0 && j < N)
- {
- for(u = 0; u < g[i].size(); ++ u)
- {
- for(v = 0; v < g[j].size(); ++ v)
- {
- if(d[3][ g[i][u] ][ g[j][v] ]==0)
- {
- ++ top;
- PR(g[i][u]*as[4]+g[j][v]);
- if(top==m)
- goto x;
- d[3][ g[i][u] ][ g[j][v] ] = 1;
- }
- }
- }
- }
- }
- }
- x:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement