QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185536 | #4907. djq 学生物 | hos_lyric# | 10 | 78ms | 5272kb | C++14 | 8.5kb | 2023-09-22 11:12:33 | 2024-07-04 02:06:43 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
vector<vector<Mint>> inverse(vector<vector<Mint>> a) {
const int n = a.size();
vector<vector<Mint>> b(n, vector<Mint>(n, 0));
for (int i = 0; i < n; ++i) b[i][i] = 1;
for (int h = 0; h < n; ++h) {
for (int i = h; i < n; ++i) if (a[i][h]) {
swap(a[h], a[i]);
swap(b[h], b[i]);
break;
}
// assert(a[h][h]);
if (!a[h][h]) return {};
const Mint s = a[h][h].inv();
for (int j = h + 1; j < n; ++j) a[h][j] *= s;
for (int j = 0; j < n; ++j) b[h][j] *= s;
for (int i = h + 1; i < n; ++i) {
const Mint t = a[i][h];
if (t) {
for (int j = h + 1; j < n; ++j) a[i][j] -= t * a[h][j];
for (int j = 0; j < n; ++j) b[i][j] -= t * b[h][j];
}
}
}
for (int h = n; --h >= 0; ) for (int i = 0; i < h; ++i) {
const Mint t = a[i][h];
if (t) for (int j = 0; j < n; ++j) b[i][j] -= t * b[h][j];
}
return b;
}
// solution: a[j][n]
bool solveEq(vector<vector<Mint>> &a) {
const int n = a.size();
for (int h = 0; h < n; ++h) {
for (int i = h; i < n; ++i) if (a[i][h]) {
swap(a[h], a[i]);
break;
}
if (!a[h][h]) return false;
const Mint s = a[h][h].inv();
for (int j = h + 1; j <= n; ++j) a[h][j] *= s;
for (int i = h + 1; i < n; ++i) {
for (int j = h + 1; j <= n; ++j) a[i][j] -= a[i][h] * a[h][j];
}
}
for (int i = n; --i >= 0; ) {
for (int j = i + 1; j < n; ++j) a[i][n] -= a[i][j] * a[j][n];
}
return true;
}
int SIX[9];
int INV[6], MUL[6][6];
int N;
vector<Mint> C;
Mint M, invM;
/*
Pr[u -> v] = (1/M) [u = v] + \sum[i] (C[i]/M) Pr[u i -> v]
ans[v] = Pr[0 -> v]
Pr[u -> v] =: f(v^-1 u)
f(u) = (1/M) [u = id] + \sum[i] (C[i]/M) f(u i)
ans[v] = f(v^-1)
*/
namespace brute {
vector<Mint> run() {
vector<vector<Mint>> a(SIX[N], vector<Mint>(SIX[N] + 1, 0));
a[0][SIX[N]] += invM;
for (int u = 0; u < SIX[N]; ++u) {
a[u][u] += 1;
for (int i = 0; i < SIX[N]; ++i) {
int v = 0;
for (int e = 0; e < N; ++e) {
const int p = u / SIX[e] % 6;
const int q = i / SIX[e] % 6;
v += MUL[p][q] * SIX[e];
}
// if(C[i])cerr<<u<<" "<<i<<": "<<v<<endl;
a[u][v] -= invM * C[i];
}
}
// for(int u=0;u<SIX[N];++u)cerr<<a[u]<<endl;
const bool res = solveEq(a);
if (res) {
vector<Mint> ans(SIX[N]);
for (int u = 0; u < SIX[N]; ++u) {
ans[u] = a[u][SIX[N]];
}
return ans;
} else {
return {};
}
}
} // brute
namespace fast {
vector<Mint> run() {
vector<Mint> ans(SIX[N], 0);
ans[0] = invM;
for (int e = 0; e < N; ++e) {
vector<vector<Mint>> a(6, vector<Mint>(6, 0));
for (int p = 0; p < 6; ++p) {
a[p][p] += 1;
for (int i = 0; i < SIX[N]; ++i) {
const int q = i / SIX[e] % 6;
a[p][MUL[p][q]] -= invM * C[i];
}
}
const auto b = inverse(a);
if (b.empty()) {
return {};
}
Mint fs[6], gs[6];
for (int u = 0; u < SIX[N]; ++u) if (u / SIX[e] % 6 == 0) {
for (int p = 0; p < 6; ++p) fs[p] = ans[u + SIX[e] * p];
fill(gs, gs + 6, 0);
for (int p = 0; p < 6; ++p) for (int q = 0; q < 6; ++q) gs[p] += b[p][q] * fs[q];
for (int p = 0; p < 6; ++p) ans[u + SIX[e] * p] = gs[p];
}
}
return ans;
}
} // fast
int main() {
SIX[0] = 1;
for (int e = 1; e < 9; ++e) SIX[e] = SIX[e - 1] * 6;
cerr<<"SIX = ";pv(SIX,SIX+9);
{
vector<vector<int>> perms;
{
vector<int> perm{0, 1, 2};
do {
perms.push_back(perm);
} while (next_permutation(perm.begin(), perm.end()));
}
map<vector<int>, int> tr;
for (int p = 0; p < 6; ++p) {
tr[perms[p]] = p;
}
for (int p = 0; p < 6; ++p) {
vector<int> perm(3);
for (int i = 0; i < 3; ++i) {
perm[perms[p][i]] = i;
}
INV[p] = tr[perm];
}
for (int p = 0; p < 6; ++p) for (int q = 0; q < 6; ++q) {
vector<int> perm(3);
for (int i = 0; i < 3; ++i) {
perm[i] = perms[p][ perms[q][i] ];
}
MUL[p][q] = tr[perm];
}
}
cerr<<"INV = ";pv(INV,INV+6);
for(int p=0;p<6;++p){cerr<<"MUL["<<p<<"] = ";pv(MUL[p],MUL[p]+6);}
for (; ~scanf("%d", &N); ) {
C.resize(SIX[N]);
for (int i = 0; i < SIX[N]; ++i) {
scanf("%u", &C[i].x);
}
M = 1;
for (int i = 0; i < SIX[N]; ++i) {
M += C[i];
}
assert(M);
invM = Mint(M).inv();
const auto ans = fast::run();
if (!ans.empty()) {
unsigned key = 0;
for (int u = 0; u < SIX[N]; ++u) {
key ^= ans[u].x;
}
printf("%u\n", key);
} else {
puts("-1");
}
#ifdef LOCAL
const auto brt=brute::run();
cerr<<"brt = "<<brt<<endl;
cerr<<"ans = "<<ans<<endl;
assert(brt==ans);
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 3672kb
input:
1 10 10 10 10 499122217 499122156
output:
-1
result:
ok 1 number(s): "-1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
1 719885386 651516139 596516649 191397068 26958009 352245674
output:
320270701
result:
ok 1 number(s): "320270701"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3868kb
input:
1 783368690 104275706 48409057 969269573 366936187 542139073
output:
252144401
result:
ok 1 number(s): "252144401"
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #4:
score: 0
Wrong Answer
time: 1ms
memory: 3780kb
input:
2 304089172 305211383 35005211 521595368 294702567 728712076 336465782 861021530 278722862 233665123 148685361 468703135 103269576 803735449 317389669 635723058 370888716 127653814 61717040 92529750 628175011 658233689 132931876 655133020 859484421 916300566 608413784 756898537 736330845 975349971 1...
output:
978145883
result:
wrong answer 1st numbers differ - expected: '396724238', found: '978145883'
Subtask #3:
score: 0
Wrong Answer
Test #8:
score: 0
Wrong Answer
time: 78ms
memory: 5272kb
input:
7 13237606 0 0 696947386 879320747 0 0 0 0 0 0 0 0 0 0 0 0 0 266959993 0 0 371358373 632390641 0 666960563 0 0 708812199 564325578 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 299649176 0...
output:
447234104
result:
wrong answer 1st numbers differ - expected: '101942575', found: '447234104'
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%