#include <stdio.h> #include <stdlib.h> #include <math.h> long long pot2(long long p) { if (p == 0) return 1; if (p == 1) return 2; if (p%2==1) return pot2((p-1)/2)*pot2((p-1)/2)*2; else return pot2(p/2)*pot2(p/2); //return 2 << ((long long)p-1); } int countSetBits(long long n) { int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main() { short n; // Liczba zestawow long long n_2; short a[40]; short z[40][40]; short pary0 = 0; short pary1 = 0; long long zgrzyt[40*40]; short c_zgrzyt = 0; short xx[40*40]; scanf("%hd",&n); n_2 = 2 << (n-1); //printf("n=%hd n^2=%lld\n",n,n_2); for (int i = 0; i<n; i++) { scanf("%hd",&a[i]); } for (int i = 1; i<n; i++) { for (int j = 0; j<i; j++) { if (a[i] < a[j]) z[i][j] = 1; else z[i][j] = 0; if (z[i][j] == 0) { pary0++; } if (z[i][j] == 1) { pary1++; zgrzyt[c_zgrzyt] = pot2(i)+pot2(j); //printf("z %hd,%hd = %lld\n",i+1,j+1,zgrzyt[c_zgrzyt]); c_zgrzyt++; } } } //for (int i = 1; i<n; i++) { // for (int j = 0; j<i; j++) { // printf("%hd ",z[i][j]); // } // printf("\n"); //} // k == 0 printf("0 %hd\n",n); // k == 1 if (n > 1) { if (pary0 > 0) printf("0 %hd\n",pary0); else printf("1 %hd\n",pary1); } for (int k = 2; k<n; k++) { for (int s=0;s<40*40;s++) xx[s]=0; for (long long i = 0; i < n_2; i++) { if (countSetBits(i) == k+1) { short x = 0; for (int z = 0; z < c_zgrzyt; z++) { if (((i)|zgrzyt[z]) == (i)) x++; } xx[x]++; //printf(">%lld x=%hd\n",i,x); } } //for (int s = 0; s<30; s++) printf("%hd ",xx[s]); //printf("\n"); short s = 0; while (s<40*40 && xx[s] == 0) s++; printf("%hd %hd\n",s,xx[s]); } 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 92 93 94 | #include <stdio.h> #include <stdlib.h> #include <math.h> long long pot2(long long p) { if (p == 0) return 1; if (p == 1) return 2; if (p%2==1) return pot2((p-1)/2)*pot2((p-1)/2)*2; else return pot2(p/2)*pot2(p/2); //return 2 << ((long long)p-1); } int countSetBits(long long n) { int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main() { short n; // Liczba zestawow long long n_2; short a[40]; short z[40][40]; short pary0 = 0; short pary1 = 0; long long zgrzyt[40*40]; short c_zgrzyt = 0; short xx[40*40]; scanf("%hd",&n); n_2 = 2 << (n-1); //printf("n=%hd n^2=%lld\n",n,n_2); for (int i = 0; i<n; i++) { scanf("%hd",&a[i]); } for (int i = 1; i<n; i++) { for (int j = 0; j<i; j++) { if (a[i] < a[j]) z[i][j] = 1; else z[i][j] = 0; if (z[i][j] == 0) { pary0++; } if (z[i][j] == 1) { pary1++; zgrzyt[c_zgrzyt] = pot2(i)+pot2(j); //printf("z %hd,%hd = %lld\n",i+1,j+1,zgrzyt[c_zgrzyt]); c_zgrzyt++; } } } //for (int i = 1; i<n; i++) { // for (int j = 0; j<i; j++) { // printf("%hd ",z[i][j]); // } // printf("\n"); //} // k == 0 printf("0 %hd\n",n); // k == 1 if (n > 1) { if (pary0 > 0) printf("0 %hd\n",pary0); else printf("1 %hd\n",pary1); } for (int k = 2; k<n; k++) { for (int s=0;s<40*40;s++) xx[s]=0; for (long long i = 0; i < n_2; i++) { if (countSetBits(i) == k+1) { short x = 0; for (int z = 0; z < c_zgrzyt; z++) { if (((i)|zgrzyt[z]) == (i)) x++; } xx[x]++; //printf(">%lld x=%hd\n",i,x); } } //for (int s = 0; s<30; s++) printf("%hd ",xx[s]); //printf("\n"); short s = 0; while (s<40*40 && xx[s] == 0) s++; printf("%hd %hd\n",s,xx[s]); } return 0; } |