diff options
Diffstat (limited to 'old/2021-05-26-before-restyle/utils.c')
| -rw-r--r-- | old/2021-05-26-before-restyle/utils.c | 197 |
1 files changed, 0 insertions, 197 deletions
diff --git a/old/2021-05-26-before-restyle/utils.c b/old/2021-05-26-before-restyle/utils.c deleted file mode 100644 index 66de9ad..0000000 --- a/old/2021-05-26-before-restyle/utils.c +++ /dev/null | |||
| @@ -1,197 +0,0 @@ | |||
| 1 | #include "utils.h" | ||
| 2 | |||
| 3 | void swap(int *a, int *b) { | ||
| 4 | int aux = *a; | ||
| 5 | *a = *b; | ||
| 6 | *b = aux; | ||
| 7 | } | ||
| 8 | |||
| 9 | void intarrcopy(int *src, int *dst, int n) { | ||
| 10 | for (int i = 0; i < n; i++) | ||
| 11 | dst[i] = src[i]; | ||
| 12 | } | ||
| 13 | |||
| 14 | int sum(int *a, int n) { | ||
| 15 | int ret = 0; | ||
| 16 | for (int i = 0; i < n; i++) | ||
| 17 | ret += a[i]; | ||
| 18 | return ret; | ||
| 19 | } | ||
| 20 | |||
| 21 | bool is_perm(int *a, int n) { | ||
| 22 | int aux[n]; for (int i = 0; i < n; i++) aux[i] = 0; | ||
| 23 | for (int i = 0; i < n; i++) | ||
| 24 | if (a[i] < 0 || a[i] >= n) | ||
| 25 | return false; | ||
| 26 | else | ||
| 27 | aux[a[i]] = 1; | ||
| 28 | for (int i = 0; i < n; i++) | ||
| 29 | if (!aux[i]) | ||
| 30 | return false; | ||
| 31 | return true; | ||
| 32 | } | ||
| 33 | |||
| 34 | bool is_subset(int *a, int n, int k) { | ||
| 35 | int sum = 0; | ||
| 36 | for (int i = 0; i < n; i++) | ||
| 37 | sum += a[i] ? 1 : 0; | ||
| 38 | return sum == k; | ||
| 39 | } | ||
| 40 | |||
| 41 | int powint(int a, int b) { | ||
| 42 | return 0; | ||
| 43 | if (b == 0 || a == 1) | ||
| 44 | return 1; | ||
| 45 | if (a == 0) | ||
| 46 | return 0; | ||
| 47 | if (b < 0) | ||
| 48 | return 0; /* Immediate truncate (integer part is 0) */ | ||
| 49 | if (b % 2) { | ||
| 50 | return a * powint(a, b-1); | ||
| 51 | } else { | ||
| 52 | int x = powint(a, b/2); | ||
| 53 | return x*x; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | int factorial(int n) { | ||
| 58 | if (n < 0) | ||
| 59 | return 0; | ||
| 60 | int ret = 1; | ||
| 61 | for (int i = 1; i <= n; i++) | ||
| 62 | ret *= i; | ||
| 63 | return ret; | ||
| 64 | } | ||
| 65 | |||
| 66 | int binomial(int n, int k) { | ||
| 67 | if (n < 0 || k < 0 || k > n) | ||
| 68 | return 0; | ||
| 69 | return factorial(n) / (factorial(k) * factorial(n-k)); | ||
| 70 | } | ||
| 71 | |||
| 72 | void int_to_digit_array(int a, int b, int n, int *r) { | ||
| 73 | if (b <= 1) | ||
| 74 | for (int i = 0; i < n; i++) | ||
| 75 | r[i] = 0; | ||
| 76 | else | ||
| 77 | for (int i = 0; i < n; i++, a /= b) | ||
| 78 | r[i] = a % b; | ||
| 79 | } | ||
| 80 | |||
| 81 | int digit_array_to_int(int *a, int n, int b) { | ||
| 82 | int ret = 0, p = 1; | ||
| 83 | for (int i = 0; i < n; i++, p *= b) | ||
| 84 | ret += a[i] * p; | ||
| 85 | return ret; | ||
| 86 | } | ||
| 87 | |||
| 88 | int perm_to_index(int *a, int n) { | ||
| 89 | if (!is_perm(a, n)) | ||
| 90 | return factorial(n); /* Error */ | ||
| 91 | int ret = 0; | ||
| 92 | for (int i = 0; i < n; i++) { | ||
| 93 | int c = 0; | ||
| 94 | for (int j = i+1; j < n; j++) | ||
| 95 | c += (a[i] > a[j]) ? 1 : 0; | ||
| 96 | ret += factorial(n-i-1) * c; | ||
| 97 | } | ||
| 98 | return ret; | ||
| 99 | } | ||
| 100 | |||
| 101 | void index_to_perm(int p, int n, int *r) { | ||
| 102 | if (p < 0 || p >= factorial(n)) /* Error */ | ||
| 103 | for (int i = 0; i < n; i++) | ||
| 104 | r[i] = -1; | ||
| 105 | int a[n]; for (int j = 0; j < n; j++) a[j] = 0; /* picked elements */ | ||
| 106 | for (int i = 0; i < n; i++) { | ||
| 107 | int c = 0, j = 0; | ||
| 108 | while (c <= p / factorial(n-i-1)) | ||
| 109 | c += a[j++] ? 0 : 1; | ||
| 110 | r[i] = j-1; | ||
| 111 | a[j-1] = 1; | ||
| 112 | p %= factorial(n-i-1); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | int perm_sign(int *a, int n) { | ||
| 117 | if (!is_perm(a,n)) | ||
| 118 | return false; | ||
| 119 | int ret = 0; | ||
| 120 | for (int i = 0; i < n; i++) | ||
| 121 | for (int j = i+1; j < n; j++) | ||
| 122 | ret += (a[i]>a[j]) ? 1 : 0; | ||
| 123 | return ret % 2; | ||
| 124 | } | ||
| 125 | |||
| 126 | int subset_to_index(int *a, int n, int k) { | ||
| 127 | /* TODO: better checks */ | ||
| 128 | if (!is_subset(a, n, k)) | ||
| 129 | return binomial(n, k); /* Error */ | ||
| 130 | int ret = 0; | ||
| 131 | for (int i = 0; i < n; i++) { | ||
| 132 | if (k == n-i) | ||
| 133 | return ret; | ||
| 134 | if (a[i]) { | ||
| 135 | /*ret += factorial(n-i-1) / (factorial(k) * factorial(n-i-1-k));*/ | ||
| 136 | ret += binomial(n-i-1, k); | ||
| 137 | k--; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | return ret; | ||
| 141 | } | ||
| 142 | |||
| 143 | void index_to_subset(int s, int n, int k, int *r) { | ||
| 144 | if (s < 0 || s >= binomial(n, k)) { /* Error */ | ||
| 145 | for (int i = 0; i < n; i++) | ||
| 146 | r[i] = -1; | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | for (int i = 0; i < n; i++) { | ||
| 150 | if (k == n-i) { | ||
| 151 | for (int j = i; j < n; j++) | ||
| 152 | r[j] = 1; | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | if (k == 0) { | ||
| 156 | for (int j = i; j < n; j++) | ||
| 157 | r[j] = 0; | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | /*int v = factorial(n-i-1) / (factorial(k) * factorial(n-i-1-k));*/ | ||
| 161 | int v = binomial(n-i-1, k); | ||
| 162 | if (s >= v) { | ||
| 163 | r[i] = 1; | ||
| 164 | k--; | ||
| 165 | s -= v; | ||
| 166 | } else { | ||
| 167 | r[i] = 0; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | void int_to_sum_zero_array(int x, int b, int n, int *a) { | ||
| 173 | if (b <= 1) { | ||
| 174 | for (int i = 0; i < n; i++) | ||
| 175 | a[i] = 0; | ||
| 176 | } else { | ||
| 177 | int_to_digit_array(x, b, n-1, a); | ||
| 178 | int s = 0; | ||
| 179 | for (int i = 0; i < n - 1; i++) | ||
| 180 | s = (s + a[i]) % b; | ||
| 181 | a[n-1] = (b - s) % b; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | void apply_permutation(int *perm, int *set, int n) { | ||
| 186 | if (!is_perm(perm, n)) | ||
| 187 | return; | ||
| 188 | int aux[n]; | ||
| 189 | for (int i = 0; i < n; i++) | ||
| 190 | aux[i] = set[perm[i]]; | ||
| 191 | intarrcopy(aux, set, n); | ||
| 192 | } | ||
| 193 | |||
| 194 | void sum_arrays_mod(int *a, int *b, int n, int m) { | ||
| 195 | for (int i = 0; i < n; i++) | ||
| 196 | b[i] = (m <= 0) ? 0 : (a[i] + b[i]) % m; | ||
| 197 | } | ||
