QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#800900#8821. NightmareNotNotToday#WA 1ms5744kbC++204.1kb2024-12-06 16:37:492024-12-06 16:37:51

Judging History

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

  • [2024-12-06 16:37:51]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5744kb
  • [2024-12-06 16:37:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 505;
const int P = 1e6 + 7;

int p;

int add(int a, int b) {
    return a + b < p ? a + b : a + b - p;
}

int sub(int a, int b) {
    return a - b >= 0 ? a - b : a - b + p;
}

int mul(int a, int b) {
    return a * 1ll * b % p;
}

int pw(int a, int n) {
    int ret = 1;
    for (; n > 0; n >>= 1, a = mul(a, a)) {
        if (n & 1) {
            ret = mul(ret, a);
        }
    }
    return ret;
}

struct elem_op {
    int type, i, j, coef;
};

int n;
int a[N][N];
int v[N][N];
vector<elem_op> ops;
int root[P];
int m;

void row_add(int i, int j, int coef) {
    for (int k = 0; k < n; ++k) {
        a[i][k] = add(a[i][k], mul(a[j][k], coef));
    }
}

void row_swap(int i, int j) {
    for (int k = 0; k < n; ++k) {
        swap(a[i][k], a[j][k]);
    }
}

void col_add(int i, int j, int coef) {
    for (int k = 0; k < n; ++k) {
        a[k][i] = add(a[k][i], mul(a[k][j], coef));
    }
}

void col_swap(int i, int j) {
    for (int k = 0; k < n; ++k) {
        swap(a[k][i], a[k][j]);
    }
}

void multi_add(int i, int j, int coef) {
    row_add(i, j, coef);
    col_add(i, j, coef);
}

void multi_swap(int i, int j) {
    row_swap(i, j);
    col_swap(i, j);
}

void v_row_add(int i, int j, int coef) {
    for (int k = 0; k < m; ++k) {
        v[i][k] = add(v[i][k], mul(v[j][k], coef));
    }
}

void v_row_swap(int i, int j) {
    for (int k = 0; k < m; ++k) {
        swap(v[i][k], v[j][k]);
    }
}

void v_col_add(int i, int j, int coef) {
    for (int k = 0; k < m; ++k) {
        v[k][i] = add(v[k][i], mul(v[k][j], coef));
    }
}

void v_col_swap(int i, int j) {
    for (int k = 0; k < m; ++k) {
        swap(v[k][i], v[k][j]);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> p;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }

    for (int k = 0; k < n; ++k) {
        if (a[k][k] == 0) {
            bool ok = 1;
            for (int i = k + 1; i < n; ++i) {
                ok &= a[k][i] == 0;
            }
            if (ok) {
                continue;
            }
            if (p == 2) {
                for (int i = k + 1; i < n; ++i) {
                    if (a[i][i] != 0) {
                        ok = 1;
                        multi_swap(k, i);
                        ops.push_back({1, k, i, -1});
                        break;
                    }
                }
                assert(ok);
            } else {
                for (int i = k + 1; i < n; ++i) {
                    if (a[k][i] != 0) {
                        multi_add(k, i, 1);
                        ops.push_back({0, k, i, 1});
                        assert(a[k][k] != 0);
                        break;
                    }
                }
            }
        }
        int r = pw(a[k][k], p - 2);
        for (int i = k + 1; i < n; ++i) {
            if (a[k][i] != 0) {
                int coef = sub(0, mul(a[k][i], r));
                multi_add(i, k, coef);
                ops.push_back({0, i, k, coef});
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (a[i][i] == 0 && a[j][j] != 0) {
                multi_swap(i, j);
                ops.push_back({1, i, j, -1});
            }
        }
    }
    // for (int i = 0; i < n; ++i) {
    //     for (int j = 0; j < n; ++j) {
    //         cout << a[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    for (int i = 0; i < n; ++i) {
        if (a[i][i] != 0) {
            ++m;
        }
    }
    for (int i = 0; i < p; ++i) {
        root[mul(i, i)] = i;
    }
    for (int i = 0; i < m; ++i) {
        v[i][i] = root[a[i][i]];
    }
    reverse(ops.begin(), ops.end());
    for (auto op : ops) {
        if (op.type == 0) {
            v_col_add(op.i, op.j, op.coef);
            v_row_add(op.i, op.j, op.coef);
        } else {
            v_col_swap(op.i, op.j);
            v_row_swap(op.i, op.j);
        }
    }
    cout << m << "\n";
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cout << v[i][j] << " ";
        }
        cout << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5744kb

input:

2 2
1 1
1 1

output:

1
1 
1 

result:

ok accepted

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3712kb

input:

3 5
4 4 3
4 4 3
3 3 2

output:

2
3 2 
2 3 
4 0 

result:

wrong answer wrong answer