const int maxn = 310;
struct Mat
{
int n, m;
bool v[maxn][maxn];
Mat (int n = 0, int m = 0, int zero = 0)
{
this->n = n;
this->m = m;
if (zero)
REP(i, n) REP(j, m)
v[i][j] = false;
}
} ipt, now;
Mat mul(Mat& a, Mat& b)
{
Mat ret(a.n, b.m, true);
REP(i, a.n) REP(k, b.m) REP(j, a.m)
{
ret.v[i][k] ^= (a.v[i][j] & b.v[j][k]);
}
return ret;
}
Mat qpow(Mat& a, int b)
{
Mat ret(a.n, a.n);
int cnt = 0;
while (b)
{
if (b & 1)
{
if (cnt == 0)
{
cnt = 1;
ret = a;
}
else
ret = mul(ret, a);
}
b >>= 1;
a = mul(a, a);
}
return ret;
}
bool a[maxn][maxn];
int gauss(int N, int M)
{
int r, c, pvt;
bool flag;
for (r = 0, c = 0; r < N && c < M; r++, c++)
{
flag = false;
for (int i = r; i < N; i++)
if (a[i][c])
{
flag = a[pvt = i][c];
break;
}
if (!flag)
{
r--;
continue;
}
if (pvt != r)
for (int j = r; j <= M; j++)
swap(a[r][j], a[pvt][j]);
for (int i = r + 1; i < N; ++i) {
if (a[i][c])
{
a[i][c] = false;
for (int j = c + 1; j <= M; ++j)
if (a[r][j])
a[i][j] = !a[i][j];
}
}
}
for (int i = r; i < N; i++)
if (a[i][M])
return -1;
if (r < M)
return M - r;
for (int i = M - 1; i >= 0; i--)
{
for (int j = i + 1; j < M; j++)
if (a[i][j])
a[i][M] ^= a[j][M];
a[i][M] /= a[i][i];
}
return 0;
}
int main()
{
int n, T;
while (~RI(n))
{
ipt.n = ipt.m = now.m = n;
now.n = 1;
REP(i, n) REP(j, n)
RI(ipt.v[i][j]);
REP(i, n)
RI(a[i][n]);
RI(T);
ipt = qpow(ipt, T);
REP(i, n) REP(j, n)
a[i][j] = ipt.v[i][j];
int ret = gauss(n, n);
if (ret == 0)
REP(i, n)
printf("%d%c", a[i][n], (i == n - 1 ? '\n' : ' '));
else if (ret > 0)
puts("ambiguous");
else
puts("none");
}
return 0;
}原文地址:http://blog.csdn.net/wty__/article/details/38148593