제출 #758548

#제출 시각아이디문제언어결과실행 시간메모리
758548rainboy앨리스, 밥, 서킷 (APIO23_abc)C++17
66 / 100
84 ms15484 KiB
#include "abc.h"
#include <cstring>

const int N = 30;
const int M = 1000;
const int L = 16;
const int LN = 5;	/* LN = ceil(log2(N)) */
const int OP_INPUT   = -1;
const int OP_ZERO    = 0;  // f(OP_ZERO,    x0, x1) = 0
const int OP_NOR     = 1;  // f(OP_NOR,     x0, x1) = !(x0 || x1)
const int OP_GREATER = 2;  // f(OP_GREATER, x0, x1) = (x0 > x1)
const int OP_NOT_X1  = 3;  // f(OP_NOT_X1,  x0, x1) = !x1
const int OP_LESS    = 4;  // f(OP_LESS,    x0, x1) = (x0 < x1)
const int OP_NOT_X0  = 5;  // f(OP_NOT_X0,  x0, x1) = !x0
const int OP_XOR     = 6;  // f(OP_XOR,     x0, x1) = (x0 ^ x1)
const int OP_NAND    = 7;  // f(OP_NAND,    x0, x1) = !(x0 && x1)
const int OP_AND     = 8;  // f(OP_AND,     x0, x1) = (x0 && x1)
const int OP_EQUAL   = 9;  // f(OP_EQUAL,   x0, x1) = (x0 == x1)
const int OP_X0      = 10; // f(OP_X0,      x0, x1) = x0
const int OP_GEQ     = 11; // f(OP_GEQ,     x0, x1) = (x0 >= x1)
const int OP_X1      = 12; // f(OP_X1,      x0, x1) = x1
const int OP_LEQ     = 13; // f(OP_LEQ,     x0, x1) = (x0 <= x1)
const int OP_OR      = 14; // f(OP_OR,      x0, x1) = (x0 || x1)
const int OP_ONE     = 15; // f(OP_ONE,     x0, x1) = 1

#define OPS	op, uu, k

unsigned int AX;

int alice_rand() {
	return (AX *= 3) >> 1;
}

void alice_sort(const char name[][5], int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + alice_rand() % (r - l)], tmp;

		while (j < k) {
			int c = strcmp(name[ii[j]], name[i_]);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		alice_sort(name, ii, l, i);
		l = k;
	}
}

int alice(const int n, const char name[][5], const unsigned short xx[], bool aa[]) {
	if (n == 1) {
		for (int h = 0; h < L; h++)
			aa[h] = xx[0] >> h & 1;
		return L;
	} else {
		int ii[N];
		for (int i = 0; i < n; i++)
			ii[i] = i;
		AX = 12345, alice_sort(name, ii, 0, n);
		for (int i = 0; i < n; i++) {
			for (int l = 0; l < LN; l++)
				aa[i * (LN + L) + l] = ii[i] >> l & 1;
			for (int l = 0; l < L; l++)
				aa[i * (LN + L) + (LN + l)] = xx[ii[i]] >> l & 1;
		}
		return n * (LN + L);
	}
}

unsigned int BX;

int bob_rand() {
	return (BX *= 3) >> 1;
}

void bob_sort(const char name[][5], int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + bob_rand() % (r - l)], tmp;

		while (j < k) {
			int c = strcmp(name[ii[j]], name[i_]);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		bob_sort(name, ii, l, i);
		l = k;
	}
}

int bob(const int m, const char uu[][5], const char vv[][5], bool bb[]) {
	for (int l = 0; l < L; l++)
		bb[l] = m >> l & 1;
	char name[M * 2][5];
	int hh[M * 2], ii[M * 2];
	for (int h = 0; h < m; h++)
		strcpy(name[h << 1 | 0], uu[h]), strcpy(name[h << 1 | 1], vv[h]);
	for (int h = 0; h < m * 2; h++)
		hh[h] = h;
	BX = 12345, bob_sort(name, hh, 0, m * 2);
	int n = 0;
	for (int h = 0; h < m * 2; h++)
		ii[hh[h]] = h + 1 == m * 2 || strcmp(name[hh[h + 1]], name[hh[h]]) != 0 ? n++ : n;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			bb[L + i * n + j] = 0;
	for (int h = 0; h < m; h++)
		bb[L + ii[h << 1 | 0] * n + ii[h << 1 | 1]] = 1;
	return L + n * n;
}

int gate(int op[], int uu[][2], int &k, int o, int u, int v) {
	op[k] = o, uu[k][0] = u, uu[k][1] = v;
	return k++;
}

void add1(int op[], int uu[][2], int &k, int *cc, int u, int i) {
	for (int j = i; j < L; j++) {
		int c = gate(OPS, OP_XOR, cc[j], u), v = gate(OPS, OP_AND, cc[j], u);
		cc[j] = c, u = v;
	}
}

void mult(int op[], int uu[][2], int &k, int *aa, int *bb, int *cc) {
	for (int i = 0; i < L; i++)
		for (int j = 0; i + j < L; j++)
			add1(OPS, cc, gate(OPS, OP_AND, aa[i], bb[j]), i + j);
}

int circuit(const int ka, const int kb, int op[], int uu[][2], int outputs_circuit[][16]) {
	int k = 0;
	for (int h = 0; h < ka + kb; h++)
		gate(OPS, OP_INPUT, -1, -1);
	int ZERO = gate(OPS, OP_ZERO, 0, 0), ONE = gate(OPS, OP_ONE, 0, 0);
	if (ka == L) {
		int aa[L], bb[L], cc[L];
		for (int l = 0; l < L; l++)
			aa[l] = l, bb[l] = L + l;
		for (int l = 0; l < L; l++)
			cc[l] = ZERO;
		mult(OPS, aa, bb, cc);
		for (int l = 0; l < L; l++)
			outputs_circuit[0][l] = cc[l];
		return k;
	} else {
		int n = 0;
		while (L + n * n < kb)
			n++;
		int cc[N][L];
		for (int i = 0; i < n; i++)
			for (int l = 0; l < L; l++)
				cc[i][l] = ZERO;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				for (int l = 0; l < L; l++)
					add1(OPS, cc[j], gate(OPS, OP_AND, ka + L + i * n + j, i * (LN + L) + LN + l), l);
		for (int i = 0; i < n; i++)
			for (int l = 0; l < L; l++)
				outputs_circuit[i][l] = ZERO;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) {
				int eq = ONE;
				for (int l = 0; l < LN; l++)
					eq = gate(OPS, OP_AND, eq, gate(OPS, OP_EQUAL, i * (LN + L) + l, (j & 1 << l) == 0 ? ZERO : ONE));
				for (int l = 0; l < L; l++)
					add1(OPS, outputs_circuit[j], gate(OPS, OP_AND, eq, cc[i][l]), l);
			}
		return k;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...