#include <stdio.h> void rev(int n, int *a) { int t[n]; for (int i = 0; i < n; i++)t[i] = *(a + n - i - 1); for (int i = 0; i < n; i++)*(a + i) = t[i]; } int f(int n, int *a) { if (n == 1)return 0; int wyn = f(n - 1, a + 1); if (wyn) return wyn; int is = 0, mini = 1e6; for (int i = 1; i < n; i++) { if (mini > a[i] && a[i] > *a) { mini = a[i]; is = i; } } if (mini == 1e6)return 0; int x = *a; *a = a[is]; a[is] = x; rev(n - 1, a + 1); return 1; } int main() { int n,w=0,k; int s=0; scanf("%d%d", &n,&k); for(int i=1;i<n;i++)s+=i; if(s%2==1){ printf("NIE"); return 0; } int a[n]; for (int i = 0; i < n; i++)a[i] = i + 1; while (1) { int q=0; for (int i = 0; i < n; i++){ //printf("%d ", a[i]); for(int j=0; j<i;j++)if(a[j]> a[i])q++; } //printf(" :%d\n",q); if(q==(s/2))w++; if(w==k){ printf("TAK\n"); for(int x=0;x<n;x++)printf("%d ",a[x]); return 0; } if (!f(n, a))break; } printf("NIE"); }
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 | #include <stdio.h> void rev(int n, int *a) { int t[n]; for (int i = 0; i < n; i++)t[i] = *(a + n - i - 1); for (int i = 0; i < n; i++)*(a + i) = t[i]; } int f(int n, int *a) { if (n == 1)return 0; int wyn = f(n - 1, a + 1); if (wyn) return wyn; int is = 0, mini = 1e6; for (int i = 1; i < n; i++) { if (mini > a[i] && a[i] > *a) { mini = a[i]; is = i; } } if (mini == 1e6)return 0; int x = *a; *a = a[is]; a[is] = x; rev(n - 1, a + 1); return 1; } int main() { int n,w=0,k; int s=0; scanf("%d%d", &n,&k); for(int i=1;i<n;i++)s+=i; if(s%2==1){ printf("NIE"); return 0; } int a[n]; for (int i = 0; i < n; i++)a[i] = i + 1; while (1) { int q=0; for (int i = 0; i < n; i++){ //printf("%d ", a[i]); for(int j=0; j<i;j++)if(a[j]> a[i])q++; } //printf(" :%d\n",q); if(q==(s/2))w++; if(w==k){ printf("TAK\n"); for(int x=0;x<n;x++)printf("%d ",a[x]); return 0; } if (!f(n, a))break; } printf("NIE"); } |