【NOI Online 3】Magic

題目解法

考慮分別處理答案的每一位,則不難發現,異或可以看做模 22 意義下的線性變換。
預處理轉移矩陣的次冪,在詢問時矩陣乘向量即可。

有關矩陣和向量的運算可以通過 bitset 壓位優化。

時間複雜度 O(N3LogVw+QN2Log2Vw)O(\frac{N^3LogV}{w}+\frac{QN^2Log^2V}{w}) ,其中 w=64w=64

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int MAXLOG = 32;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
struct mat {bitset <MAXN> r[MAXN], c[MAXN]; };
int n, m, q; mat a[MAXLOG];
unsigned v[MAXN];
mat operator * (mat a, mat b) {
	mat ans;
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++) {
		bool res = (a.r[i] & b.c[j]).count() % 2 == 1;
		ans.r[i][j] = ans.c[j][i] = res;
	}
	return ans;
}
int main() {
	freopen("magic.in", "r", stdin);
	freopen("magic.out", "w", stdout);
	read(n), read(m), read(q);
	for (int i = 1; i <= n; i++)
		read(v[i]);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		a[0].r[x][y] = true;
		a[0].r[y][x] = true;
		a[0].c[x][y] = true;
		a[0].c[y][x] = true;
	}
	for (int i = 1; i <= 31; i++)
		a[i] = a[i - 1] * a[i - 1];
	for (int i = 1; i <= q; i++) {
		unsigned t, ans = 0; read(t);
		for (int j = 0; j <= 31; j++) {
			bitset <MAXN> vec;
			for (int k = 1; k <= n; k++)
				if ((1u << j) & v[k]) vec[k] = true;
				else vec[k] = false;
			for (int k = 0; k <= 31; k++)
				if ((1u << k) & t) {
					bitset <MAXN> tmp;
					for (int l = 1; l <= n; l++)
						tmp[l] = (vec & a[k].c[l]).count() % 2;
					vec = tmp;
				}
			if (vec[1]) ans ^= 1u << j;
		}
		cout << ans << endl;
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章