QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#522966 | #8830. Breaking Bad | smosp# | WA | 370ms | 11704kb | C++20 | 3.9kb | 2024-08-17 17:34:32 | 2024-08-17 17:34:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int a, int b) {
int x = abs((int)rng());
return x % (b - a + 1) + a;
}
const int T = 10;
vi brute_force(int n, vector<vi> &v, int rem) {
vi p(n);
iota(all(p), 0);
vi ans(5, 0);
do {
int x = rem;
rep(i, 0, n) x += v[i][p[i]];
ans[x % 5] = 1;
} while(next_permutation(all(p)));
return ans;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
// If n <= 10 -> Try all. Easy.
vector<vi> v(n, vi(n));
rep(i, 0, n) rep(j, 0, n) {
cin >> v[i][j];
v[i][j] %= 5;
}
if(n <= T) {
auto ans = brute_force(n, v, 0);
for(auto i : ans) cout << (i == 0 ? 'N' : 'Y');
cout << '\n';
return 0;
}
vector<vi> r(n, vi(5, 0)), c(n, vi(5, 0));
rep(i, 0, n) rep(j, 0, n) {
r[i][v[i][j]]++;
c[j][v[i][j]]++;
}
vi bestr(n), bestc(n);
iota(all(bestr), 0);
iota(all(bestc), 0);
sort(all(bestr), [&](int a, int b) {
int suma = 0;
int sumb = 0;
// int nonzeroa = 0;
// int nonzerob = 0;
rep(i, 0, 5) {
suma += r[a][i] * r[a][i];
sumb += r[b][i] * r[b][i];
// if(r[a][i] != 0) nonzeroa++;
// if(r[b][i] != 0) nonzerob++;
}
return suma < sumb;
});
sort(all(bestc), [&](int a, int b) {
int suma = 0;
int sumb = 0;
rep(i, 0, 5) {
suma += c[a][i] * c[a][i];
sumb += c[b][i] * c[b][i];
}
return suma < sumb;
});
// Now we can try random cases based on this "variation" index.
int rem = 0;
rep(i, T, n) {
rem += v[bestr[i]][bestc[i]];
}
vector<vi> t(T, vi(T));
rep(i, 0, T) rep(j, 0, T) {
t[i][j] = v[bestr[i]][bestc[j]];
}
auto ans = brute_force(T, t, rem);
{
reverse(all(bestr));
reverse(all(bestc));
int rem = 0;
rep(i, T, n) {
rem += v[bestr[i]][bestc[i]];
}
vector<vi> t(T, vi(T));
rep(i, 0, T) rep(j, 0, T) {
t[i][j] = v[bestr[i]][bestc[j]];
}
auto ans2 = brute_force(T, t, rem);
rep(i, 0, 5) ans[i] += ans2[i];
}
reverse(all(bestr));
reverse(all(bestc));
rep(_, 0, 4) {
int tt = min(T*2, n);
shuffle(bestr.begin(), bestr.begin() + tt, rng);
shuffle(bestc.begin(), bestc.begin() + tt, rng);
int rem = 0;
rep(i, T, n) {
rem += v[bestr[i]][bestc[i]];
}
vector<vi> t(T, vi(T));
rep(i, 0, T) rep(j, 0, T) {
t[i][j] = v[bestr[i]][bestc[j]];
}
auto ans2 = brute_force(T, t, rem);
rep(i, 0, 5) ans[i] += ans2[i];
}
rep(_, 0, 4) {
shuffle(all(bestr), rng);
shuffle(all(bestc), rng);
int rem = 0;
rep(i, T, n) {
rem += v[bestr[i]][bestc[i]];
}
vector<vi> t(T, vi(T));
rep(i, 0, T) rep(j, 0, T) {
t[i][j] = v[bestr[i]][bestc[j]];
}
auto ans2 = brute_force(T, t, rem);
rep(i, 0, 5) ans[i] += ans2[i];
}
// rep(_, 0, 4) {
// // Pick random T * T
// int tt = min(T*2, n);
// set <int> r, c;
// while(sz(r) < T) r.insert(rand(0, tt - 1));
// while(sz(c) < T) c.insert(rand(0, tt - 1));
// int rem = 0;
// vector<int> vr(tt, 0);
// vector<int> vc(tt, 0);
// for(auto i : r) vr[i]++;
// for(auto i : c) vc[i]++;
// int ri = 0;
// int ci = 0;
// while(ri < n) {
// if(vr[ri] == 1) {
// ri++;
// continue;
// }
// if(vc[ci] == 1) {
// ci++;
// continue;
// }
// rem += v[bestr[ri]][bestc[ci]];
// ri++;
// ci++;
// }
// vector<vi> u(T, vi(T));
// vi vvr; for(auto i : r) vvr.push_back(i);
// vi vvc; for(auto i : c) vvc.push_back(i);
// rep(i, 0, T) rep(j, 0, T) u[i][j] = v[bestr[vvr[i]]][bestc[vvc[j]]];
// auto ans2 = brute_force(T, t, rem);
// rep(i, 0, 5) ans[i] += ans2[i];
// }
for(auto i : ans) cout << (i == 0 ? 'N' : 'Y');
cout << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
2 0 4 4 0
output:
YNNYN
result:
ok "YNNYN"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
2 1 1 1 1
output:
NNYNN
result:
ok "NNYNN"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
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: 0ms
memory: 3620kb
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: 34ms
memory: 3776kb
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: 34ms
memory: 3672kb
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: 34ms
memory: 3616kb
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: 34ms
memory: 3676kb
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: 35ms
memory: 3616kb
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: 34ms
memory: 3688kb
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: 34ms
memory: 3680kb
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: 35ms
memory: 3612kb
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: 34ms
memory: 3560kb
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: 34ms
memory: 3616kb
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: 0
Accepted
time: 363ms
memory: 11704kb
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:
NNNYN
result:
ok "NNNYN"
Test #16:
score: -100
Wrong Answer
time: 370ms
memory: 11632kb
input:
1000 2 3 0 1 0 0 0 1 1 4 1 4 2 3 0 3 4 2 3 2 4 2 1 1 1 1 0 0 3 3 2 0 2 2 2 4 3 0 3 3 3 0 1 3 0 2 0 1 0 0 0 3 1 4 3 1 4 0 3 4 4 1 3 3 4 0 1 4 2 3 0 1 0 0 2 3 1 4 0 0 2 1 2 3 3 4 4 3 3 2 0 0 4 3 2 3 3 2 4 0 2 2 3 0 3 2 4 1 0 2 4 2 4 1 2 1 3 0 3 3 0 1 1 0 2 3 0 2 4 1 4 3 4 1 2 2 2 4 0 1 3 3 0 0 1 3 2 4...
output:
NYNNN
result:
wrong answer 1st words differ - expected: 'NYYYY', found: 'NYNNN'