vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res; int **tmp = newint*[n]; for (int i = 0; i < n; i++) tmp[i] = newint[n]; int i = 0, j = 0; int base_i = 0, base_j = 0; int value = 1; int m = n; int p = n;
while (true) { while (j < p) { tmp[i][j] = value++; j++; } j = p - 1; base_i++; if (base_i == m)break; i = base_i; while (i < m) { tmp[i][j] = value++; i++; } i = m - 1; p--; if (p == base_j)break; j = p - 1; while (j >= base_j) { tmp[i][j] = value++; j--; } j = base_j; m--; if (base_i == m)break; i = m - 1; while (i >= base_i) { tmp[i][j] = value++; i--; } i = base_i; base_j++; if (base_j == p)break; j = base_j; } for (int i = 0; i < n; i++) { vector<int> k; for (int j = 0; j < n; j++) k.push_back(tmp[i][j]); res.push_back(k); } return res; }
intfac(int n) { if (n == 1)return1; return n * fac(n - 1); } string res = ""; voidgetNext(vector<char>& number, int n, int k) { if (n == 1) { res = res + number[0]; return; } int f = fac(n - 1); int w = (k - 1) / f; res = res + number[w]; number.erase(number.begin() + w); getNext(number, n - 1, k-w*f); } stringgetPermutation(int n, int k){ vector<char> number; for (int i = 1; i <= n; i++) number.push_back('0'+i); getNext(number, n, k); return res; }