QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185479 | #4907. djq 学生物 | hos_lyric# | 0 | 0ms | 0kb | C++14 | 6.8kb | 2023-09-22 09:06:08 | 2024-07-04 02:45:44 |
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>;
// 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 {
assert(false);
return {};
}
}
} // brute
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 = brute::run();
#ifdef LOCAL
cerr<<"ans = "<<ans<<endl;
#endif
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");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
1 10 10 10 10 499122217 499122156
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #8:
score: 0
Time Limit Exceeded
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:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%