Discovering Permutations Solution
#include<bits/stdc++.h>
#define N 28
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int used[N], alphabet[N], cnt, c;
void permutation(int pos, int n)
{
if(c >= cnt)
return;
if(pos == n+1)
{
c++;
for(int i=1; i<=n; i++)
{
char ch = alphabet[i]+64;
cout << ch;
}
cout << "\n";
return;
}
for(int i=1; i<=n; i++)
{
if(used[i] == 0)
{
used[i] = 1;
alphabet[pos] = i;
permutation(pos+1, n);
used[i] = 0;
}
}
}
int main()
{
fast
int t, k = 1;
cin >> t;
while(t--)
{
int n;
cin >> n >> cnt;
cout << "Case " << k++ << ":\n";
c = 0;
permutation(1, n);
}
return 0;
}
Comments
Post a Comment