QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708564#2931. Tri-Color PuzzleBackToSquare1AC ✓8ms3996kbC++205.6kb2024-11-03 23:55:202024-11-03 23:55:22

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 23:55:22]
  • 评测
  • 测评结果:AC
  • 用时:8ms
  • 内存:3996kb
  • [2024-11-03 23:55:20]
  • 提交

answer

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
 
typedef pair<ll, ll> pl;
typedef pair<ld,ld> pd;
typedef vector<ll> vl;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
 
 
#define G(x) ll x; cin >> x;
#define F(i, l, r) for (ll i = l; i < (r); ++i)
#define all(a) begin(a), end(a)
#define K first
#define V second
#define OK(i,j) i >= 0 && i < n && j >= 0 && j < m
 
#define NN 2505
#define MM 5005
#define MOD 1000000007
#define M 3

ll powmod(ll x, ll y){
  ll r=1;
  while (y) {
    if (y%2) r = r*x%M;
    x = x*x%M;
    y/=2;
  }
  return r;
}

ll powmod(ll x, ll y, ll m) {
  ll r=1;
  while (y) {
    if (y%2) r = r*x%m;
    x = x*x%m;
    y/=2;
  }
  return r;
}

ll inv(ll a, ll b){return 1<a ? b - inv(b%a,a)*b/a : 1;}

ll inv(ll a) {return inv(a, M);}

typedef vector<vector<ll>> mat;

mat operator*(const mat &a, const mat &b) {
    mat c(a.size(), vector<ll>(b[0].size()));

    for (int i=0; i<a.size(); ++i)
        for (int k=0; k<b.size(); ++k)
            for (int j=0; j<b[0].size(); ++j)
                ( c[i][j] += a[i][k]*b[k][j] )%=M;
    return c;
}

mat operator+(mat a, const mat&b) {
    for (int i=0; i<a.size(); ++i)
        for (int j=0; j<a[0].size(); ++j)
            ( a[i][j] += b[i][j] )%=M;
    return a;
}

mat operator*(ll f, mat a) {
    for (int i=0; i<a.size(); ++i)
        for (int j=0; j<a[0].size(); ++j)
            ( a[i][j] *= f )%=M;
    return a;
}

mat operator*(const mat &a, ll f) {return f*a;}
mat operator/(const mat &a, ll f) {return a*inv(f);}
mat operator-(const mat &a){return a*(M-1);}
mat operator-(const mat &a, const mat &b) {return a + -b;}

mat transpose(const mat &a) {
    mat b(a[0].size(), vector<ll>(a.size()));
    for (int i=0; i<a.size(); ++i)
        for (int j=0; j<a[0].size(); ++j)
            b[j][i] = a[i][j];
    return b;
}

mat mat_id(int n) {
    mat a(n, vector<ll>(n));
    for (ll i=0; i<n; ++i) a[i][i]=1;
    return a;
}

mat exp_mat(const mat &x, ll y) {
    if (y==0) return mat_id(x.size());
    mat t = exp_mat(x, y/2);
    if (y%2) return t*t*x;
    return t*t;
}

// https://cp-algorithms.com/linear_algebra/linear-system-gauss.html
vector<int> where;
int solve_system(mat &a, vector<ll> &b) {
    int n=a.size();
    int m=a[0].size();
    for (int i=0; i<n; ++i) a[i].push_back(b[i]);

    where.assign(m, -1);
    for (int col=0, row=0; col<m && row<n; ++col) {
        for (int i=row; i<n; ++i)
            if (a[i][col]) {
                swap(a[i], a[row]);
                break;
            }
        if (!a[row][col])
            continue;
        where[col] = row;

        for (int i=0; i<n; ++i)
            if (i != row) {
                ll c = a[i][col] * inv(a[row][col]) %M;
                for (int j=col; j<=m; ++j)
                    a[i][j] = ( a[i][j] - a[row][j]*c%M + M)%M;
            }
        ++row;
    }

    b.assign(m, 0);
    for (int i=0; i<m; ++i)
        if (where[i] != -1)
            b[i] = a[where[i]][m] * inv(a[where[i]][i]) %M;
    for (int i=0; i<n; ++i) {
        ll sum = 0;
        for (int j=0; j<m; ++j)
           ( sum += b[j] * a[i][j] )%=M;
        if (sum != a[i][m])
            return 0;
    }

    for (int i=0; i<m; ++i)
        if (where[i] == -1)
            return 2;
    return 1;
}

map<pl,ll> pair_to_ll;
map<ll,pl> ll_to_pair;

void solve() {
    
    ll S, I;
    cin >> S >> I;
    ll idx = 0;
    for(ll i=0;i<S;i++) {
        for(ll j=0;j<=i;j++) {
            pair_to_ll[{i,j}] = idx;
            ll_to_pair[idx] = {i,j};
            idx++;
        }
    }

    mat data(S*(S-1)/2);
    vector<ll> b(S*(S-1)/2);
    set<ll> given;
    map<ll,ll> color;
    for(ll i=0;i<S*(S-1)/2;i++) {
        b[i] = 0;
        for(ll j=0;j<S*(S+1)/2;j++) {
            data[i].push_back(0);
            if(j == i || j == pair_to_ll[{ll_to_pair[i].K+1,ll_to_pair[i].V}] || j == pair_to_ll[{ll_to_pair[i].K+1,ll_to_pair[i].V+1}]) data[i][j] = 1;
        }
    }

    // for(ll i=0;i<S*(S-1)/2;i++) {
    //     for(ll j=0;j<S*(S+1)/2;j++) {
    //         cout << data[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }

    ll u, v, cc;

    for(ll i=0;i<I;i++) {
        cin >> u >> v >> cc;
        u--; v--;
        ll c = pair_to_ll[{u,v}];
        if(given.count(c) > 0 && color[c] != cc) {
            cout << 0 << '\n';
            return;
        }
        given.insert(c);
        color[c] = cc;
        for(ll j=0;j<S*(S-1)/2;j++) {
            if(data[j][c] == 1) {
                data[j][c] = 0;
                b[j] = (b[j]+M-cc)%M;
            }
        }
    }

    int z = solve_system(data,b);
    if(z == 0) {
        cout << 0 << '\n';
        return;
    }

    // for(ll i=0;i<S*(S-1)/2;i++) {
    //     for(ll j=0;j<S*(S+1)/2;j++) {
    //         cout << data[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }

    ll i = 0, j = 0;
    ll ans = 1;
    while(i < S*(S-1)/2 && j < S*(S+1)/2) {
        if(given.count(j) > 0) j++;
        else if(data[i][j] == 0) {
            ans *= 3;
            j++;
        }
        else {
            i++;
            j++;
        }

    }

    while(j < S*(S+1)/2) {
        if(given.count(j) == 0) ans *= 3;
        j++;
    }

    cout << ans << '\n';
    

}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cout << fixed << setprecision(10);

    solve();

    return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3624kb

input:

4 4
1 1 0
2 1 2
4 1 2
4 4 2

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

4 4
1 1 1
2 1 2
4 1 2
3 3 2

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

4 4
1 1 0
2 1 1
4 1 0
4 4 0

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

6 4
1 1 0
2 2 0
5 1 1
5 5 2

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 4ms
memory: 3860kb

input:

17 16
3 2 2
4 4 0
5 2 1
6 4 1
8 5 2
8 8 2
11 1 2
11 7 0
12 4 2
12 10 2
12 11 0
13 11 2
14 5 2
14 8 0
16 7 0
17 5 1

output:

3

result:

ok single line: '3'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

11 9
2 2 2
5 3 0
6 5 2
9 7 1
10 10 1
11 4 1
11 7 0
11 10 1
11 11 2

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

4 4
2 1 1
4 1 2
4 3 1
4 4 2

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 3ms
memory: 3764kb

input:

16 7
6 3 0
9 5 0
11 11 2
12 7 1
13 10 2
14 7 0
15 10 1

output:

19683

result:

ok single line: '19683'

Test #9:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

10 4
4 3 2
8 2 2
8 6 1
9 5 0

output:

729

result:

ok single line: '729'

Test #10:

score: 0
Accepted
time: 5ms
memory: 3960kb

input:

18 29
4 1 2
6 5 0
7 3 2
7 5 0
8 6 1
9 1 0
9 6 1
10 1 0
10 2 0
10 3 0
10 6 1
11 2 2
11 8 2
11 11 0
13 8 0
14 3 1
14 5 1
14 6 1
14 9 0
14 12 0
15 4 1
15 7 1
15 11 1
15 14 2
16 8 1
16 9 0
16 15 2
18 6 1
18 16 2

output:

1

result:

ok single line: '1'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

5 5
3 2 0
3 3 1
4 1 1
5 1 0
5 3 0

output:

1

result:

ok single line: '1'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3692kb

input:

11 8
4 3 0
6 3 1
6 5 0
6 6 1
7 3 2
10 3 0
10 7 0
11 11 2

output:

27

result:

ok single line: '27'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3508kb

input:

6 6
2 2 1
3 1 0
3 3 1
5 5 1
6 1 1
6 5 1

output:

1

result:

ok single line: '1'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3916kb

input:

11 7
3 2 0
3 3 2
5 1 0
7 3 2
10 1 1
10 6 2
10 8 0

output:

81

result:

ok single line: '81'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

8 7
3 1 2
4 2 1
5 2 0
5 3 1
5 4 0
6 3 2
8 8 2

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 8ms
memory: 3996kb

input:

19 5
9 7 0
11 10 0
18 9 0
19 10 1
19 17 0

output:

4782969

result:

ok single line: '4782969'

Test #17:

score: 0
Accepted
time: 1ms
memory: 3632kb

input:

11 9
7 3 1
7 5 0
7 6 2
8 4 0
8 7 2
9 3 0
10 3 0
10 9 0
11 1 2

output:

0

result:

ok single line: '0'