Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 9 + 5;
- string str;
- int cnt;
- bool visited[N];
- bool recur(int n, int k, int i){
- if(i == 0){
- ++cnt;
- if(cnt == k){
- return true;
- }
- return false;
- }
- for(int j = 0; j < n; ++j){
- if(!visited[j]){
- str += (char)j + 1 + '0';
- visited[j] = true;
- if(recur(n, k, i - 1)){
- return true;
- }
- str.pop_back();
- visited[j] = false;
- }
- }
- return false;
- }
- string getPermutation(int n, int k){
- if(recur(n, k, n)){
- return str;
- }
- return "";
- }
- int main(){
- int n, k;
- scanf("%d%d", &n, &k);
- cout << getPermutation(n, k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement