#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
unsigned long long int silnia(int n)
{
long long int a = 1;
while(n)
a *= n--;
return a;
}
int main()
{
int t[250003];
int n;
long long int k;
scanf("%d %lld",&n,&k);
int inwersje_suma=n*(n-1)/2;
//printf("%d",inwersje_suma);
if(inwersje_suma%2==1)
{
printf("NIE\n");
}else{
for(int i=0;i<n;i++)
{
t[i]=i+1;
}
int count_inw;
int count_kol=0;
int mark_tak=0;
while (true)
{
count_inw=0;
/*for (int i = 0; i < n; i++)
printf("%d" , t[i]);*/
//printf("\n");
for(int i = 0; i < n; i++)
{
for(int j= i; j < n; j++)
{
if(t[i]>t[j])
count_inw++;
}
}
//printf(" %d\n",count_inw);
if(count_inw==inwersje_suma/2)
{
count_kol++;
if(count_kol==k)
{
printf("TAK\n%");
for(int i=0;i<n-1;i++)
printf("%d ",t[i]);
printf("%d\n",t[n-1]);
mark_tak=1;
break;
}
}
int i = n - 1;
while (i > 0 && t[i - 1] >= t[i])
i--;
if (i == 0)
break;
int j = i;
while (j < n && t[j] > t[i - 1])
j++;
j--;
swap(t[i - 1], t[j]);
reverse(t + i, t + n);
}
if(mark_tak==0)
{
printf("NIE\n");
}
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <cstdio> #include <cmath> #include <algorithm> using namespace std; unsigned long long int silnia(int n) { long long int a = 1; while(n) a *= n--; return a; } int main() { int t[250003]; int n; long long int k; scanf("%d %lld",&n,&k); int inwersje_suma=n*(n-1)/2; //printf("%d",inwersje_suma); if(inwersje_suma%2==1) { printf("NIE\n"); }else{ for(int i=0;i<n;i++) { t[i]=i+1; } int count_inw; int count_kol=0; int mark_tak=0; while (true) { count_inw=0; /*for (int i = 0; i < n; i++) printf("%d" , t[i]);*/ //printf("\n"); for(int i = 0; i < n; i++) { for(int j= i; j < n; j++) { if(t[i]>t[j]) count_inw++; } } //printf(" %d\n",count_inw); if(count_inw==inwersje_suma/2) { count_kol++; if(count_kol==k) { printf("TAK\n%"); for(int i=0;i<n-1;i++) printf("%d ",t[i]); printf("%d\n",t[n-1]); mark_tak=1; break; } } int i = n - 1; while (i > 0 && t[i - 1] >= t[i]) i--; if (i == 0) break; int j = i; while (j < n && t[j] > t[i - 1]) j++; j--; swap(t[i - 1], t[j]); reverse(t + i, t + n); } if(mark_tak==0) { printf("NIE\n"); } } return 0; } |
English