From d85ee21d5bd4e8e59e6f0cb1d2ce0f2aeff11e5c Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 10 Mar 2022 23:21:37 +0300 Subject: [PATCH] cache, flag --- cpu/cache.c | 45 +++++++++++++++++++++++++ cpu/flag.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 cpu/cache.c create mode 100644 cpu/flag.c diff --git a/cpu/cache.c b/cpu/cache.c new file mode 100644 index 0000000..824d1de --- /dev/null +++ b/cpu/cache.c @@ -0,0 +1,45 @@ +#include +#define N 1024 + +int gcd(int a, int b); +int main() { + int n, d; + int i, j, k; + /* to use shorter types such as short is a BAD idea */ + static int A[N][N], B[N][N], C[N][N]; + int res = 0; + + scanf("%d %d", &n, &d); + + for (i = 0; i < n; ++i) + for (j = i; j < n; ++j) { + //printf("%d %d\n", i ,j); + A[i][j] = gcd(i + 2,j + 2); + A[j][i] = A[i][j]; + } + //printf("nya!\n"); + for (i = 0; i < n; ++i) + for (j = i; j < n; ++j) { + //printf("%d %d\n", i ,j); + B[i][j] = gcd(n - i,n - j); + B[j][i] = B[i][j]; + } + + for (i = 0; i < n; ++i) + for (j = 0; j < n; ++j) + for (k = 0; k < n; ++k) { + //printf("i:%d, j^%d, k^%d\n", i, j, k); + C[i][j] += A[i][k] * B[j][k]; + } + for (i = 0; i < n; ++i) + for (j = 0; j < n; ++j) + if (C[i][j] % d == 0) + ++res; + + printf("%d", res); +} + +int gcd(int a, int b) { + return (a == 0) ? b : gcd(b % a, a); +} + diff --git a/cpu/flag.c b/cpu/flag.c new file mode 100644 index 0000000..a41ec7e --- /dev/null +++ b/cpu/flag.c @@ -0,0 +1,94 @@ +#include +#include + +int check_registers(int a, int b, int max_reg); +int main() { + int max_reg = 4; + int zf = 0, to_exec = 1; +#ifdef REGISTERS + max_reg = REGISTERS; +#endif + /* A, B, C, D. Indice - 5 */ + uint8_t reg[4] = {0, 0, 0, 0}; + int i; + uint8_t a, b; + uint8_t x; + + while (1) {; + scanf("%hhu", &x); +#ifdef _DEBUG + printf("x:%hhu\n", x); +#endif + if ((x & (1 << 7)) && !zf) { + to_exec = 0; + //zf = 0; + } + x &= 0x7F; +#ifdef _DEBUG + printf("zf:%d, to_exec:%d\n", zf, to_exec); +#endif + if (to_exec == 0) { + if ((x >= 1) && (x <= 3)) + scanf("%hhu %hhu\n", &a, &b); + to_exec = 1; + zf = 0; + continue; + } + + switch (x) { + case 0: + return 0; + break; + case 1: + scanf("%hhu %hhu\n", &a, &b); + if (check_registers(a, b, max_reg)) { + printf("-1\n"); + return 0; + } + reg[a-5] += reg[b-5]; + if (reg[a-5] == 0) + zf = 1; + else + zf = 0; + break; + case 2: + scanf("%hhu %hhu\n", &a, &b); + if(check_registers(a, b, max_reg)) { + printf("-1\n"); + return 0; + } + reg[a-5] -= reg[b-5]; + if (reg[a-5] == 0) + zf = 1; + else + zf = 0; + break; + case 3: + scanf("%hhu %hhu\n", &a, &b); + if(check_registers(a, 0, max_reg)) { + printf("-1\n"); + return 0; + } + reg[a-5] = b; + if (reg[a-5] == 0) + zf = 1; + else + zf = 0; + break; + case 4: + for (i = 0; i < max_reg; ++i) + printf("%hhu ", reg[i]); + putchar('\n'); + break; + } + to_exec = 1; + } +} + +int check_registers(int a, int b, int max_reg) { + a -= 5; + b -= 5; + if ((a >= max_reg) || (b >= max_reg)) + return 1; + return 0; +}