Advertisement
Guest User

Untitled

a guest
Nov 12th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define eb emplace_back
  4. #define pb push_back
  5. #define fi first
  6. #define se second
  7.  
  8. using namespace std;
  9. typedef long long ll;
  10. typedef pair<int, int> ii;
  11.  
  12. const int N = 65536 + 7;
  13.  
  14. stack<int> e[N];
  15. vector<int> path;
  16. void dfs(int u) {
  17.     int v;
  18.     while(e[u].size()) {
  19.         v = e[u].top();
  20.         e[u].pop();
  21.         dfs(v);
  22.     }
  23.     path.pb(u);
  24. }
  25. int main() {
  26.     ios::sync_with_stdio(0); cin.tie(0);
  27.  
  28.     int n, m;
  29.     while(cin >> m >> n) {
  30.         if(m == 1) {
  31.             for(int i = 0; i < n; ++i) cout << i;
  32.             cout << '\n';
  33.             continue;
  34.         }
  35.  
  36.         int tot = pow(n, m-1);
  37.         // cria os nós
  38.  
  39.         path.clear();
  40.         for(int i = 0; i < tot; ++i) {
  41.             while(e[i].size()) e[i].pop();
  42.             // cout << "Arestas de i = " << i << endl;
  43.             for(int j = 0; j < n; ++j) {
  44.                 e[i].emplace((i*n)%tot + j);
  45.                 // cout << (i*n)%tot + j << ' ';
  46.             }
  47.             // cout << endl;
  48.         }
  49.  
  50.         dfs(0);
  51.         reverse(path.begin(), path.end());
  52.         path.pop_back();
  53.         for(int x : path) {
  54.             cout << x%n;
  55.         }
  56.         cout << endl;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement