Graph automata player

Source: Internet
Author: User

Question here

The first fast power is also the first Gaussian elimination element.

The relational matrix of the input edge is the coefficient matrix Co.

[CO] ^ t * [ANS] = (the status of the current 0-moment), [CO] ^ t can be quickly obtained by the matrix power

The-T moment state is the value of the ANS matrix, which can be obtained by Gaussian elimination.

Just judge.

The matrix of the coefficients in the Gaussian elimination element is a [0... n-1] [0... m-1] The constant matrix is a [0... n-1] [m]

If the return value is-1, there is no solution. If it is equal to 0, there is a unique solution. If it is greater than 0, it indicates the number of uncertain variables.


#include <cstdio>#include <ctime>#include <cstdlib>#include <cstring>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <vector>#include <iostream>#include <algorithm>#include <bitset>#include <fstream>using namespace std;//LOOP#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)//OTHER#define SZ(V) (int)V.size()#define PB push_back#define MP make_pair#define all(x) (x).begin(),(x).end()//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)#define RS(s) scanf("%s", s)//OUTPUT#define WI(n) printf("%d\n", n)#define WS(n) printf("%s\n", n)//debug//#define online_judge#ifndef online_judge#define dt(a)  << (#a) << "=" << a << " "#define debugI(a) cout dt(a) << endl#define debugII(a, b) cout dt(a) dt(b) << endl#define debugIII(a, b, c) cout dt(a) dt(b) dt(c) << endl#define debugIV(a, b, c, d) cout dt(a) dt(b) dt(c) dt(d) << endl#define debugV(a, b, c, d, e) cout dt(a) dt(b) dt(c) dt(d) dt(e) << endl#else#define debugI(v)#define debugII(a, b)#define debugIII(a, b, c)#define debugIV(a, b, c, d)#endif#define sqr(x) (x) * (x)typedef long long LL;typedef unsigned long long ULL;typedef vector <int> VI;const double eps = 1e-9;const int MOD = 1000000007;const double PI = acos(-1.0);//const int INF = 0x3f3f3f3f;const int maxn = 310;const LL INF = 0x3f3f3f3f3f3f3f3fLL;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;        }    }};Mat mul(Mat& a, Mat&b){    Mat ret(a.n, b.m, 1);    REP(i, a.n)        REP(j, b.m)            REP(k, a.m)                ret.v[i][j] ^=  (a.v[i][k] & b.v[k][j]);    return ret;}Mat qpow(Mat& a, int b){    Mat ret(a.n, a.m);    bool f = 1;    while (b)    {        if (b & 1)        {            if (f)                ret = a, f = 0;            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, x;    while (~RI(n))    {        Mat co(n, n);        REP(i, n)             REP(j, n)            {                RI(x);                co.v[i][j] = (x == 1 ? true : false);            }        REP(i, n)        {            RI(x);            a[i][n] = (x == 1 ? true : false);        }        RI(T);        co = qpow(co, T);        REP(i, n)   REP(j, n)   a[i][j] = co.v[i][j];        int ans = gauss(n, n);        if (ans == -1)            puts("none");        else if (ans)            puts("ambiguous");        else        {            REP(i, n)                printf("%d%c", a[i][n], (i == n - 1 ? '\n' : ' '));        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.