QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185571 | #4907. djq 学生物 | hos_lyric# | 65 | 1012ms | 36376kb | C++14 | 9.9kb | 2023-09-22 11:54:22 | 2024-07-04 02:06:44 |
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 subA {
vector<int> good;
void make() {
good.assign(SIX[N], 0);
for (int i = 0; i < SIX[N]; ++i) {
bool ok = true;
for (int e = 0; e < N; ++e) {
const int p = i / SIX[e] % 6;
ok = ok && (p == 0 || p == 3 || p == 4);
}
if (ok) {
good[i] = 1;
}
}
}
// x + y \omega
using F = pair<Mint, Mint>;
F add(const F &a, const F &b) {
return F(a.first + b.first, a.second + b.second);
}
F mul(const F &a, const F &b) {
const Mint t = a.second * b.second;
return F(a.first * b.first - t, a.first * b.second + a.second * b.first - t);
}
// (\omega^(pq))
const F W[3][3] = {
{F(1, 0), F(1, 0), F(1, 0)},
{F(1, 0), F(0, 1), F(-1, -1)},
{F(1, 0), F(-1, -1), F(0, 1)},
};
vector<F> fs;
void dft() {
F gs[3], hs[3];
for (int e = 0; e < N; ++e) {
for (int u = 0; u < SIX[N]; ++u) if (good[u] && u / SIX[e] % 6 == 0) {
gs[0] = fs[u + 0 * SIX[e]];
gs[1] = fs[u + 3 * SIX[e]];
gs[2] = fs[u + 4 * SIX[e]];
fill(hs, hs + 3, F(0, 0));
for (int p = 0; p < 3; ++p) for (int q = 0; q < 3; ++q) {
hs[p] = add(hs[p], mul(W[p][q], gs[q]));
}
fs[u + 0 * SIX[e]] = hs[0];
fs[u + 3 * SIX[e]] = hs[1];
fs[u + 4 * SIX[e]] = hs[2];
}
}
}
vector<Mint> run() {
fs.assign(SIX[N], F(0, 0));
fs[0].first += M;
for (int i = 0; i < SIX[N]; ++i) if (good[i]) {
fs[i].first -= C[i];
}
dft();
for (int u = 0; u < SIX[N]; ++u) if (good[u]) {
const Mint x = fs[u].first;
const Mint y = fs[u].second;
Mint d = x*x - x*y + y*y;
if (!d) {
return {};
}
d = d.inv();
fs[u] = F(d * (x - y), d * (-y));
}
dft();
const Mint invThr = Mint(3).pow(-N);
vector<Mint> ans(SIX[N], 0);
for (int u = 0; u < SIX[N]; ++u) if (good[u]) {
const Mint x = fs[u].first;
const Mint y = fs[u].second;
assert(!y);
ans[u] = invThr * x;
}
return ans;
}
} // subA
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();
subA::make();
bool speA = true;
for (int i = 0; i < SIX[N]; ++i) if (C[i]) {
speA = speA && subA::good[i];
}
cerr<<"N = "<<N<<", speA = "<<speA<<endl;
vector<Mint> ans;
if (speA) {
ans = subA::run();
} else {
ans = brute::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: 3704kb
input:
1 10 10 10 10 499122217 499122156
output:
-1
result:
ok 1 number(s): "-1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3824kb
input:
1 719885386 651516139 596516649 191397068 26958009 352245674
output:
320270701
result:
ok 1 number(s): "320270701"
Test #3:
score: 0
Accepted
time: 1ms
memory: 4112kb
input:
1 783368690 104275706 48409057 969269573 366936187 542139073
output:
252144401
result:
ok 1 number(s): "252144401"
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #4:
score: 20
Accepted
time: 0ms
memory: 3816kb
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:
396724238
result:
ok 1 number(s): "396724238"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
2 913515603 749241873 137806862 42999170 982906996 135497281 511702305 87932219 939232731 829091974 572660336 160882152 805750846 634377376 102416960 435681504 143371771 84353895 939819582 4611839 2410108 549989014 610515434 587746011 376099690 760313750 478926734 356426808 945117276 891702825 78245...
output:
870050121
result:
ok 1 number(s): "870050121"
Test #6:
score: 0
Accepted
time: 8ms
memory: 4244kb
input:
3 57511226 265850707 413305323 845749015 943947739 985965659 855636226 751454233 471103741 958053186 37896442 463480570 44162728 977716025 317097467 893822248 378465744 927612902 332328964 603570492 689682299 660260756 959997301 485560280 402724286 593209441 196709512 894429689 364228444 949102266 2...
output:
315521842
result:
ok 1 number(s): "315521842"
Test #7:
score: 0
Accepted
time: 6ms
memory: 4028kb
input:
3 349517445 588219756 858424826 59174065 995706887 824845059 66858995 625032172 387451659 471017656 564157983 298625210 296921989 59223234 801633853 557074948 382697713 476667372 72330968 260401255 296864819 774044599 697517721 4741198 952711586 337695458 798829587 758671314 67067352 719346228 84681...
output:
927050034
result:
ok 1 number(s): "927050034"
Subtask #3:
score: 15
Accepted
Test #8:
score: 15
Accepted
time: 19ms
memory: 8884kb
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:
101942575
result:
ok 1 number(s): "101942575"
Test #9:
score: 0
Accepted
time: 100ms
memory: 36376kb
input:
8 114962507 0 0 952617546 783387964 0 0 0 0 0 0 0 0 0 0 0 0 0 950188130 0 0 79845349 400660703 0 865722684 0 0 186015033 757001842 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 563538995 0...
output:
976515923
result:
ok 1 number(s): "976515923"
Test #10:
score: 0
Accepted
time: 104ms
memory: 36176kb
input:
8 125443968 0 0 825927837 967844197 0 0 0 0 0 0 0 0 0 0 0 0 0 128726374 0 0 893763697 934490504 0 811156183 0 0 90656766 98645533 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 858436076 0 ...
output:
660528054
result:
ok 1 number(s): "660528054"
Test #11:
score: 0
Accepted
time: 100ms
memory: 36252kb
input:
8 553951831 0 0 3610932 151003694 0 0 0 0 0 0 0 0 0 0 0 0 0 239318443 0 0 408922789 79644945 0 59445445 0 0 144621393 336045244 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 329064496 0 0 ...
output:
355824768
result:
ok 1 number(s): "355824768"
Subtask #4:
score: 20
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 20
Accepted
time: 1008ms
memory: 9856kb
input:
4 887790043 739693399 870246744 301313259 18507681 629005716 67544354 58092673 642897565 70732414 899481125 727551664 871859472 220238086 708355050 385234243 645860416 129326472 850844210 577801468 684900507 461005014 934205121 591855867 526169505 197643478 136123938 67642730 682939149 949635706 281...
output:
602449578
result:
ok 1 number(s): "602449578"
Test #13:
score: 0
Accepted
time: 1001ms
memory: 9944kb
input:
4 557598958 113906113 211073291 59019633 110287605 318903034 307171270 201167854 142201998 886457896 24068541 289099056 55884864 35794579 11689085 615385706 684842316 559228752 366244387 646068652 535314708 313739657 846826781 931109365 759018899 574643185 553946098 150095139 137782333 741636661 949...
output:
37335981
result:
ok 1 number(s): "37335981"
Test #14:
score: 0
Accepted
time: 1012ms
memory: 9980kb
input:
4 376545129 990894084 275573387 559810624 528344836 360116901 406141975 630960645 550842736 709965265 690253412 98881522 427656896 104902833 504114740 620019063 359704636 908644219 755230881 459810186 435557594 214555147 965394547 449697679 122566226 581267354 68455475 593798485 142522444 164230614 ...
output:
75788971
result:
ok 1 number(s): "75788971"
Subtask #5:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #15:
score: 0
Time Limit Exceeded
input:
5 927227492 433462495 682484392 792453283 515614277 572882205 869882034 134859027 597519975 814230265 964334366 928235854 270932934 360466938 859244355 704701658 346892037 476472729 774188464 647828172 144729335 72341130 766681749 361690425 376034039 468002144 849139480 955698428 333940200 373129969...