QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474313#8830. Breaking BadPhoenixRebirthTL 2ms17860kbC++145.2kb2024-07-12 17:21:072024-07-12 17:21:07

Judging History

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

  • [2024-07-12 17:21:07]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:17860kb
  • [2024-07-12 17:21:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1005;

bool res[N];
int a[N][N], mat[N][N], coef[N];
int n, m, g, G[N], mod;

inline int qpow(int a, int b, int mod) {
    int res = 1;
    while (b) {
        if (b & 1) res = 1ll * res * a % mod;
        a = 1ll * a * a % mod;
        b >>= 1;
    }
    return res;
}
inline bool check(int mod) {
    for (int i = 2; i * i <= mod; i++)
        if (mod % i == 0)
            return false;
    g = 2;
    while (1) {
        if (qpow(g, mod - 1, mod) == 1) {
            bool flag = 1;
            for (int i = 2; i * i <= mod - 1; i++) {
                if ((mod - 1) % i == 0 && (qpow(g, (mod - 1) / i, mod) == 1 || qpow(g, i, mod) == 1)) {
                    flag = 0;
                    break;
                }
            }
            if (flag) return true;
        }
        g++;
    }
}
void getG() {
    mod = (1000000000 / m + 1) * m + 1;
    while (!check(mod)) mod += m;
    // printf("g = %d, mod = %d\n", g, mod);
}

inline int my_rnd() {
    // return rng() % (mod - 1) + 1;
    return rng() % mod;
}

inline int det() {
    int res = 1;
    for (int i = 1; i <= n; i++) {
        int pilot = 0;
        for (int j = i; j <= n; j++)
            if (mat[j][i]) {
                pilot = j;
                break;
            }
        if (!pilot) return 0;
        if (pilot != i) {
            swap(mat[i], mat[pilot]);
            res = mod - res;
        }
        res = 1ll * res * mat[i][i] % mod;
        int tmp = qpow(mat[i][i], mod - 2, mod);
        for (int j = i + 1; j <= n; j++) {
            if (!mat[j][i]) continue;
            int div = 1ll * mat[j][i] * tmp % mod;
            for (int k = i; k <= n; k++)
                mat[j][k] = (mat[j][k] + mod - 1ll * div * mat[i][k] % mod) % mod;
        }
    }
    return res;
}

int rnd[N][N], pre[N][N], suf[N][N], f[N];

inline void addx(int &x, int y) {
    if ((x += y) >= mod) x -= mod;
}
inline void Lagrange() {
    memset(pre, 0, sizeof(pre));
    memset(suf, 0, sizeof(suf));
    memset(f, 0, sizeof(f));
    pre[0][1] = 1, pre[0][0] = mod - G[0];
    for (int i = 1; i < m; i++) {
        for (int j = 0; j < m; j++) {
            if (j) pre[i][j] = pre[i - 1][j - 1];
            pre[i][j] = (pre[i][j] + 1ll * (mod - G[i]) * pre[i - 1][j]) % mod;
        }
    }
    suf[m][0] = 1;
    suf[m - 1][1] = 1, suf[m - 1][0] = mod - G[m - 1];
    for (int i = m - 2; i >= 0; i--) {
        for (int j = 0; j < m; j++) {
            if (j) suf[i][j] = suf[i + 1][j - 1];
            suf[i][j] = (suf[i][j] + 1ll * (mod - G[i]) * suf[i + 1][j]) % mod;
        }
    }
    for (int i = 0; i < m; i++) {
        int COEF = 1;
        for (int j = 0; j < m; j++) {
            if (j != i)
                COEF = 1ll * COEF * (G[i] - G[j] + mod) % mod;
        }
        COEF = 1ll * coef[i] * qpow(COEF, mod - 2, mod) % mod;
        if (i == 0) {
            for (int j = 0; j < m; j++)
                addx(f[j], 1ll * COEF * suf[i + 1][j] % mod);
        } else if (i == m - 1) {
            for (int j = 0; j < m; j++)
                addx(f[j], 1ll * COEF * pre[i - 1][j] % mod);
        } else {
            for (int j = 0; j < m; j++)
                for (int k = 0; j + k < m; k++)
                    addx(f[j + k], 1ll * COEF * pre[i - 1][j] % mod * suf[i + 1][k] % mod);
        }
    }
}

void run() {
    G[0] = 1, G[1] = qpow(g, (mod - 1) / m, mod);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            rnd[i][j] = my_rnd();
        }
    }
    for (int x = 0; x < m; x++) {
        if (x > 1) G[x] = 1ll * G[x - 1] * G[1] % mod;
        // printf("When x = %d\n", x);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (a[i][j] == -1) mat[i][j] = 0;
                else mat[i][j] = 1ll * rnd[i][j] * qpow(G[x], a[i][j], mod) % mod;
                // printf("%d ", mat[i][j]);
            }
            // puts("");
        }
        coef[x] = det();
        // printf("det = %d\n", coef[x]);
    }
    Lagrange();
    // for (int i = 0; i < m; i++)
    //     printf("%d ", f[i]);
    // puts("");
    for (int i = 0; i < m; i++)
        if (f[i]) res[i] = 1;
}

namespace brute {
bool vis[N];
int lnk[N];
bool dfs(int u) {
    for (int i = 1; i <= n; i++) {
        if (!vis[i] && a[u][i] != -1 && lnk[i] != u) {
            vis[i] = 1;
            if (!lnk[i] || dfs(lnk[i])) {
                lnk[i] = u;
                return true;
            }
        }
    }
    return false;
}
void main() {
    bool bad = 0;
    for (int i = 1; i <= n; i++) {
        memset(vis, false, sizeof(bool) * (n + 1));
        if (!dfs(i)) {
            bad = 1;
            break;
        }
    }
    putchar(bad ? 'N' : 'Y');
}
}

int main() {
    scanf("%d", &n), m = 5, getG();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            scanf("%d", &a[i][j]);
    if (m == 1) { brute::main(); return 0; }
    int guess = 1;
    while (guess--) run();
    for (int i = 0; i < m; i++) {
        putchar(res[i] ? 'Y' : 'N');
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
0 4
4 0

output:

YNNYN

result:

ok "YNNYN"

Test #2:

score: 0
Accepted
time: 2ms
memory: 16372kb

input:

2
1 1
1 1

output:

NNYNN

result:

ok "NNYNN"

Test #3:

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

input:

4
0 0 1 0
0 1 0 1
0 0 0 0
1 1 0 0

output:

YYYYN

result:

ok "YYYYN"

Test #4:

score: 0
Accepted
time: 2ms
memory: 16152kb

input:

4
0 0 0 1
0 1 0 1
1 0 0 0
0 1 0 0

output:

YYYYN

result:

ok "YYYYN"

Test #5:

score: 0
Accepted
time: 2ms
memory: 17344kb

input:

10
1 4 2 0 0 2 0 1 3 3
0 3 1 4 4 1 4 0 2 2
1 4 2 0 0 2 0 1 0 3
0 3 1 4 4 1 4 0 2 2
4 2 0 3 3 0 3 4 1 1
2 0 3 1 1 3 1 2 4 4
4 2 0 3 3 0 3 4 1 1
2 0 3 1 1 3 1 2 4 4
1 4 2 0 0 2 0 1 3 3
3 1 4 2 2 4 2 3 0 0

output:

NYNNY

result:

ok "NYNNY"

Test #6:

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

input:

10
4 4 4 1 3 4 1 4 3 0
3 3 3 0 2 3 0 3 2 4
3 3 3 0 2 3 0 3 2 4
4 4 4 1 3 4 1 4 3 0
2 2 2 4 1 2 4 2 1 3
2 2 2 4 1 3 4 2 1 3
4 4 4 1 3 4 1 4 3 0
3 3 3 0 2 3 0 3 2 4
2 2 2 4 1 2 4 2 1 3
4 4 4 1 3 4 1 1 3 0

output:

YYYNY

result:

ok "YYYNY"

Test #7:

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

input:

10
1 2 0 4 2 3 4 0 2 3
0 1 4 3 1 2 3 4 1 2
4 0 3 2 0 1 2 3 0 1
1 2 0 4 2 3 4 0 2 3
3 4 2 1 4 0 1 2 4 0
0 1 4 3 1 2 3 4 1 2
2 3 1 0 3 4 0 1 3 4
3 1 1 1 4 0 1 2 4 0
1 2 0 4 2 3 4 0 2 3
1 3 0 4 2 3 4 0 2 3

output:

NYYYY

result:

ok "NYYYY"

Test #8:

score: 0
Accepted
time: 2ms
memory: 17664kb

input:

10
3 4 0 3 2 2 0 4 0 2
0 1 2 0 4 4 2 1 2 4
2 3 4 2 1 1 4 3 4 1
0 1 2 0 4 4 2 1 2 4
0 1 2 0 4 4 2 1 2 4
0 1 2 0 4 4 2 1 2 4
3 4 0 3 2 2 0 4 0 2
0 1 2 0 4 4 2 1 2 4
3 4 0 3 2 2 0 4 0 2
0 1 2 0 4 4 2 1 2 4

output:

NYNNN

result:

ok "NYNNN"

Test #9:

score: 0
Accepted
time: 2ms
memory: 15920kb

input:

10
4 1 3 1 2 0 3 2 4 4
0 2 4 2 3 1 4 3 0 0
1 1 1 1 2 0 3 2 4 1
2 4 1 4 0 3 1 0 2 2
1 3 0 3 4 2 0 4 1 1
2 4 1 4 0 3 1 0 2 2
2 4 1 4 0 3 1 0 2 2
0 2 4 2 3 1 4 3 0 0
3 0 2 1 1 4 2 1 3 3
4 1 3 1 2 0 3 2 4 4

output:

YYYYY

result:

ok "YYYYY"

Test #10:

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

input:

10
1 2 0 2 4 2 3 1 2 1
4 0 3 0 2 0 1 4 0 4
0 1 4 1 3 1 2 0 1 0
0 1 4 1 3 1 2 0 1 0
3 4 2 4 1 4 0 3 4 3
4 0 3 0 2 0 1 4 0 4
0 1 4 1 3 1 2 0 1 0
0 1 4 1 3 1 2 0 1 0
3 4 2 4 1 4 0 3 4 3
0 1 4 1 3 1 2 0 1 0

output:

NNNYN

result:

ok "NNNYN"

Test #11:

score: 0
Accepted
time: 2ms
memory: 17460kb

input:

10
1 4 1 2 1 3 3 2 1 2
0 3 0 1 0 2 2 1 0 1
0 4 0 3 0 2 2 1 0 1
1 4 1 2 1 3 3 2 1 2
4 2 4 0 4 1 1 0 4 0
1 1 1 4 1 0 3 2 1 2
0 0 0 1 0 2 2 1 0 1
2 0 2 3 2 4 4 3 2 3
2 0 2 3 2 4 4 3 2 3
2 0 2 3 2 4 4 3 2 3

output:

YYYYY

result:

ok "YYYYY"

Test #12:

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

input:

10
1 2 0 1 4 0 1 2 2 2
1 2 0 1 4 3 1 2 2 2
0 1 4 0 3 1 0 1 1 1
1 2 0 1 4 3 1 2 2 2
3 4 2 3 1 4 3 4 4 4
0 1 4 0 3 1 0 1 1 1
4 0 3 4 2 0 4 0 0 0
3 4 2 3 1 4 3 4 4 4
4 0 3 4 2 0 4 0 0 0
0 1 4 0 3 1 0 1 1 1

output:

YNYNY

result:

ok "YNYNY"

Test #13:

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

input:

10
1 3 0 0 2 1 3 4 3 3
3 3 0 0 4 1 3 4 3 3
1 1 3 3 2 4 1 2 1 1
2 4 1 1 3 2 4 0 4 4
4 1 3 3 0 4 1 2 1 1
2 4 1 1 3 2 4 0 4 4
0 2 4 4 1 0 2 3 2 2
3 0 2 2 4 3 0 1 0 0
3 0 2 2 4 3 0 1 0 0
4 2 4 4 1 0 2 3 2 2

output:

YYYNY

result:

ok "YYYNY"

Test #14:

score: 0
Accepted
time: 2ms
memory: 17432kb

input:

10
2 0 3 1 3 0 0 0 4 1
1 4 2 0 2 4 4 4 3 0
2 0 3 1 3 0 0 0 4 1
1 4 2 0 2 4 4 4 3 0
1 4 2 0 2 4 4 4 3 0
3 3 4 2 4 1 1 1 0 2
3 1 4 2 4 1 1 1 0 2
4 2 0 3 0 2 2 2 1 3
3 1 4 2 4 1 1 1 0 2
1 4 2 0 2 4 4 4 3 0

output:

YNYNN

result:

ok "YNYNN"

Test #15:

score: -100
Time Limit Exceeded

input:

1000
3 4 1 2 4 1 0 3 0 4 1 4 3 1 4 4 1 0 1 2 3 1 0 1 3 4 4 0 3 0 3 2 2 1 0 4 1 3 3 0 3 1 3 2 2 0 3 3 2 2 3 0 4 2 1 2 1 2 1 4 2 4 1 4 2 4 3 2 0 3 0 4 2 1 2 3 3 0 2 0 3 3 1 1 0 3 4 3 2 0 4 0 3 4 4 2 3 4 2 3 4 2 1 3 2 2 4 1 0 2 2 4 0 1 2 0 4 1 3 2 3 2 2 2 1 4 4 4 2 0 0 4 4 1 3 4 0 2 2 3 1 1 3 2 3 2 3 0...

output:


result: