QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#311818#7612. Matrix InversePlentyOfPenaltyTL 1446ms98432kbC++174.3kb2024-01-22 20:31:122024-01-22 20:31:14

Judging History

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

  • [2024-01-22 20:31:14]
  • 评测
  • 测评结果:TL
  • 用时:1446ms
  • 内存:98432kb
  • [2024-01-22 20:31:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef std::pair<int, int> pii;
typedef long long ll;
const int MAXN = 2011, mod = 1e9 + 7;
void Add(int &a, int t) {
    a += t;
    if (a >= mod) a -= mod;
}
ll Qpow(ll a, ll p) {
    ll res = 1;
    while (p) {
        if (p & 1) res = res * a % mod;
        a = a * a % mod, p >>= 1;
    }
    return res;
}
int a[MAXN][MAXN], ra[MAXN][MAXN], b[MAXN][MAXN], rb[MAXN][MAXN], res[MAXN][MAXN];
int f[MAXN][MAXN];
int row[MAXN], col[MAXN], cr, cc;
bool type[MAXN];

void gauss(int a[MAXN][15], int n,int m) {//rank = m , n>m
    for (int i = 1; i <= m; ++i) {
        for (int j = i; j <= n; ++j)
            if (a[j][i]) {
                std::swap(a[i], a[j]);
                break;
            }
        int inv = Qpow(a[i][i], mod - 2);
        for (int k = 1; k <= m + 1; ++k)
            a[i][k] = ll(a[i][k]) * inv % mod;
        for (int j = 1; j <= n; ++j)
            if (i != j && a[j][i]) {
                int r = a[j][i];
                for (int k = i; k <= m + 1; ++k)
                    a[j][k] = ((a[j][k] - ll(r) * a[i][k]) % mod + mod) % mod;
            }
    }
}
int eq[MAXN][15];
struct one {
    int x, y, v;
    bool operator< (const one& you)const{return x<you.x||(x==you.x&&y<you.y);}
};
std::vector<one> ans;
vector<pii> seq;
int main() {

#ifdef popteam
    // freopen("I.in", "r", stdin);
#endif
    mt19937 rd(std::chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            cin >> a[i][j], ra[i][j] = a[i][j];
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            cin >> b[i][j], rb[i][j] = b[i][j], res[i][j] = b[i][j];
    for (int i = 1; i <= n; ++i)
        f[i][i] = 1;
    int lst = rd() % n + 1;
    for (int w = 1; w <= n/2; ++w) {
        int x = rd() % n + 1;
        seq.emplace_back(pii(lst, x));
        lst = x;
    }
    for (auto [x, y] : seq)
        for (int k = 1; k <= n; ++k)
            Add(ra[y][k], ra[x][k]), Add(f[y][k], f[x][k]);
    for (int c = 1; c <= n; ++c) {
        bool err = 0;
        for (int w = 1; w <= 13; ++w) {
            int i = rd() % n + 1;
            int sum = 0;
            for (int k = 1; k <= n; ++k)
                sum = (sum + ll(ra[i][k]) * b[k][c]) % mod;
            // printf("c=%d,i=%d,sum=%d\n", i, c, sum);
            if (sum != f[i][c]) {
                err = 1;
                break;
            }
        }
        if (err) col[++cc] = c; //, printf("Err,c=%d\n", c);
    }
    assert(cc <= 12);
    memset(f, 0, sizeof f);
    for (int i = 1; i <= n; ++i)
        f[i][i] = 1;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            ra[i][j] = a[i][j];
    for (auto [x, y] : seq)
        for (int k = 1; k <= n; ++k)
            Add(ra[k][y], ra[k][x]), Add(f[k][y], f[k][x]);
    for (int r = 1; r <= n; ++r) {
        bool err = 0;
        for (int w = 1; w <= 13; ++w) {
            int j = rd() % n + 1;
            int sum = 0;
            for (int k = 1; k <= n; ++k)
                sum = (sum + ll(b[r][k]) * ra[k][j]) % mod;
            if (sum != f[r][j]) {
                err = 1;
                break;
            }
        }
        if (err) row[++cr] = r, type[r] = 1; //, printf("Err,r=%d\n", r);
    }
    fprintf(stderr, "cr=%d\n", cr);
    assert(cr <= 12);
    for (int w = 1; w <= cc; ++w) {
        memset(eq, 0, sizeof eq);
        int c = col[w];
        for (int i = 1; i <= n; ++i) {
            if (i == c) ++eq[i][cr + 1];
            int cur = 0;
            for (int r = 1; r <= n; ++r)
                if (type[r])
                    eq[i][++cur] = a[i][r];
                else
                    eq[i][cr + 1] = ((eq[i][cr + 1] - ll(a[i][r]) * b[r][c]) % mod + mod) % mod;
        }
        gauss(eq, n,cr);
        int cur = 0;
        for (int r = 1; r <= n; ++r)
            if (type[r]) {
                ++cur;
                if (res[r][c] != eq[cur][cr + 1]) ans.emplace_back(one{r, c, eq[cur][cr + 1]});
            }
    }
    std::sort(ans.begin(),ans.end());
    cout << ans.size() << '\n';
    for (auto P : ans)
        cout << P.x << " " << P.y << " " << P.v << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 1326ms
memory: 98228kb

input:

1995
586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...

output:

2
827 238 84815305
1466 499 206940592

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 1336ms
memory: 98228kb

input:

1995
436890614 28924575 276129332 63568266 576410175 399540058 591733285 531509939 637241038 596750662 811926780 760228238 317196903 751498201 993802643 102539089 382116597 233386377 974332817 495280100 575832855 616941506 297856263 216480938 638907269 434126707 499611855 764625526 51141033 64624519...

output:

3
315 590 222982023
421 1912 523778307
745 1803 328256562

result:

ok 4 lines

Test #4:

score: 0
Accepted
time: 1366ms
memory: 98284kb

input:

1998
583111238 684686962 60000552 833067795 399706437 80311170 511421309 126675237 578609031 629890589 4721597 505178877 965431576 488092987 110903821 856966035 934194793 831090190 93501498 982251231 221658950 561834845 801921306 125139448 771849922 610370373 625334897 671223646 927123592 441019972 ...

output:

4
21 1273 160152585
700 1573 576757184
1674 1165 958703366
1860 1550 451190886

result:

ok 5 lines

Test #5:

score: 0
Accepted
time: 1346ms
memory: 98432kb

input:

2000
717395008 183448326 460843970 942614578 540060179 334668801 284127311 635920935 518435676 579369810 852254297 342132392 390366615 141010330 256825376 585810764 253867889 483289117 141421931 467578626 750184736 801127935 917825514 702243210 954747981 910219404 311930180 11494244 915417963 820983...

output:

5
30 992 620409139
585 130 391404065
910 139 921101622
1292 496 462362602
1579 1776 163902318

result:

ok 6 lines

Test #6:

score: 0
Accepted
time: 1436ms
memory: 98400kb

input:

2000
620840546 358023079 309405838 45500223 855686733 508959744 51181469 968972877 537705762 129000719 720633908 884983092 862573877 806580605 252024754 680227415 839963816 519838198 171117282 55679929 677376107 529124099 733179585 20941462 208815360 171000233 981430676 830346879 857681989 863823572...

output:

6
708 1628 497911789
714 1597 924600947
1105 1248 334964897
1209 1517 696980563
1386 1495 446362422
1427 979 599811696

result:

ok 7 lines

Test #7:

score: 0
Accepted
time: 1446ms
memory: 98248kb

input:

1999
199111744 347283243 902808247 73043926 77805820 474451787 854137221 262193129 508272500 493604512 479354501 851331944 65939325 676555110 301252826 583818606 235970147 545653095 120576387 825367169 249335412 365652469 200134563 609796865 481733518 594700892 34132902 906919419 939234722 888820985...

output:

7
43 586 272473841
116 1477 158203105
122 1169 761579075
477 1913 749351268
592 583 329485518
662 1400 622650552
1563 148 498180819

result:

ok 8 lines

Test #8:

score: -100
Time Limit Exceeded

input:

1999
95525499 999471184 616044299 270968642 734163712 806403756 187471687 954208629 738712543 303515182 65192240 341396506 976491262 49950446 564351227 957716298 984366409 189359888 6212233 992086125 956271984 926015141 961718572 628613990 96751949 438018145 639769590 234289696 391888974 135327482 5...

output:

8
279 1620 990329039
882 1321 138573187
1060 836 505053059
1155 214 213425345
1525 906 706361973
1553 569 257967325
1601 1141 127915131
1898 738 215879720

result: