QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#105746 | #4782. 完美的旅行 | 1234567890 | 15 | 508ms | 36956kb | C++14 | 11.7kb | 2023-05-15 11:00:24 | 2023-05-15 11:00:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define LL long long
const int N = 6e5 + 5 , mod = 998244353 , G = 3 ;
namespace BINOM {
int inv[N] , ifac[N] , fac[N] ;
int init(int n = N) {
inv[1] = fac[0] = ifac[0] = 1 ;
for (int i = 2 ; i < n ; ++ i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod ;
for (int i = 1 ; i < n ; ++ i) {
fac[i] = 1ll * fac[i - 1] * i % mod ;
ifac[i] = 1ll * ifac[i - 1] * inv[i] % mod ;
}
return 0 ;
}int get_binom = init() ;
int C(int n , int m) {
if (n < m || m < 0) return 0 ;
return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod ;
}
}
using namespace BINOM ;
int qmi(int a , int b = mod - 2) {
int res = 1; a %= mod;
while (b) {
if (b & 1) res = (long long)res * a % mod;
a = (long long)a * a % mod , b >>= 1;
}
return res;
}
inline void Add(int &x, int y) {
x += y;
if(x >= mod) x -= mod;
}
inline void Dec(int &x, int y) {
x -= y;
if(x < 0) x += mod;
}
inline int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
inline int dec(int x, int y) {
x -= y;
if(x < 0) x += mod;
return x;
}
namespace POLY {
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define V (*this)
#define SZ() ((int) size())
#define sz(a) ((int) a.size())
#define vi vector < int >
using ll = long long ;
const int MOD = mod , inv2 = (MOD + 1) / 2 , _G = G ;
int rt[N << 1], Lim;
int extend(int n) { return 1 << (__lg(n - 1) + 1) ; }
int Pinit(int x) {
for(Lim = 1; Lim < x; Lim <<= 1) ;
for(int i = 1; i < Lim; i <<= 1) {
int sG = qmi (_G, (MOD - 1) / (i << 1));
rt[i] = 1;
L(j, i + 1, i * 2 - 1) rt[j] = (ll) rt[j - 1] * sG % MOD;
}
return 0 ;
}
static int __POLY_INIT = Pinit(extend(N)) ;
struct poly : public vector<int> {
poly() : vi(){}
poly(int n , int v = 0) : vi(n , v){}
poly(vi t) : vi(t){}
poly(initializer_list<int> li) : vi(li) {}
int v(int i) { return i < 0 || i >= (int)size() ? 0 : V[i]; }
void init(int n = 0) { vi(n).swap(V); }
void clear() { init(0) ; }
inline void dif() {
int n = size() ;
for (int l = n >> 1; l >= 1; l >>= 1)
for(int j = 0; j < n; j += l << 1)
for(int k = 0, *w = rt + l; k < l; k++, w++) {
int x = V[j + k], y = V[j + k + l];
V[j + k] = add(x, y);
V[j + k + l] = (ll) * w * dec(x, y) % MOD;
}
}
void dit () {
int n = size() ;
for(int i = 2; i <= n; i <<= 1)
for(int j = 0, l = (i >> 1); j < n; j += i)
for(int k = 0, *w = rt + l; k < l; k++, w++) {
int pa = V[j + k], pb = (ll) V[j + k + l] * *w % MOD;
V[j + k] = add(pa, pb), V[j + k + l] = dec(pa, pb);
}
reverse(begin() + 1, end());
for(int i = 0, iv = qmi(n); i < n; i++) V[i] = (ll) V[i] * iv % MOD;
}
void trim() { while (!empty() && !back()) pop_back() ; }
friend poly operator * (poly aa, poly bb) {
if(!sz(aa) || !sz(bb)) return {};
// return poly(MTT::conv(aa , bb , MOD));
int all = sz(aa) + sz(bb) - 1 , lim = extend(all);
aa.resize(lim), bb.resize(lim), aa.dif(), bb.dif();
L(i, 0, lim - 1) aa[i] = (ll) aa[i] * bb[i] % MOD;
aa.dit(), aa.resize(all);
return aa;
}
poly Inv() {
assert(empty() || V[0]);
poly f, g , res = {qmi(V[0])} ;
for(int m = 1, pn; m < SZ(); m <<= 1) {
pn = m << 1, f = res, g.resize(pn), f.resize(pn);
for(int i = 0; i < pn; i++) g[i] = v(i) ;
g *= f ; for(int i = 0; i < m; i++) g[i] = 0;
g *= f , res.resize(pn);
for(int i = m; i < min(pn, SZ()); i++)
res[i] = (MOD - g[i]) % MOD;
}
return res.resize(SZ()) , res ;
}
poly operator << (int x) {
poly zm (size() + x);
L(i, 0, (int)size() - 1) zm[i + x] = V[i];
return zm;
}
poly operator >> (int x) {
if (x >= (int)size()) return poly();
poly zm (size() - x);
L(i, x , (int)size() - 1) zm[i - x] = V[i];
return zm;
}
poly shift(ll k) {
poly g = V ;
if (k > 0) {
for (int i = SZ() - 1 ; i >= k ; -- i) g[i] = g[i - k] ;
for (ll i = min((ll)SZ(), k) - 1; i >= 0 ; -- i) g[i] = 0 ;
}
else if (k < 0) {
k = -k ;
for (int i = 0 ; i + k < SZ() ; ++ i) g[i] = g[i + k] ;
for (ll i = max((ll)SZ() - k , 0ll) ; i < SZ() ; ++ i) g[i] = 0;
}
return g ;
}
friend pair<poly , poly> div (poly aa, poly bb) { //商,余
aa.trim() , bb.trim();
int n = aa.size() , m = bb.size() ;
if (n < m) return {poly() , aa} ;
poly tb = bb ;
aa.Rev() , bb.Rev() , bb.resize(n - m + 1) , bb = bb.Inv() ;
poly cc = aa * bb ; cc.resize(n - m + 1) ;
cc.Rev() , aa.Rev() ;
poly dd = aa - tb * cc ; dd.resize(m - 1) , dd.trim() ;
return {cc , dd} ;
}
friend poly operator + (poly aa, poly bb) {
poly res(max(sz(aa), sz(bb)));
L(i, 0, sz(res) - 1) res[i] = add(aa.v(i), bb.v(i));
return poly(res);
}
friend poly operator - (poly aa, poly bb) {
poly res(max(sz(aa), sz(bb)));
L(i, 0, sz(res) - 1) res[i] = dec(aa.v(i), bb.v(i));
return poly(res);
}
friend poly operator * (poly aa, int bb) {
poly res(sz(aa));
L(i, 0, sz(aa) - 1) res[i] = (ll) aa[i] * bb % MOD;
return res;
}
friend poly operator / (poly aa, poly bb) { return div(aa , bb).first ; }
friend poly operator % (poly aa, poly bb) { return div(aa , bb).second ; }
poly & operator += (poly o) {
resize(max(SZ(), sz(o)));
L(i, 0, SZ() - 1) (V[i] += o.v(i)) %= MOD;
return V ;
}
poly & operator -= (poly o) {
resize(max(SZ(), sz(o)));
L(i, 0, SZ() - 1) (V[i] += MOD - o.v(i)) %= MOD;
return V;
}
poly & operator *= (poly o) { return V = V * o; }
poly & operator /= (poly o) { return V = V / o; }
poly & operator %= (poly o) { return V = V % o; }
poly Integ() {
if(!SZ()) return poly();
poly res(SZ() + 1);
L(i, 1, SZ()) res[i] = (ll) V[i - 1] * inv[i] % MOD;
return res;
}
poly Deriv() {
if(!SZ()) return poly();
poly res(SZ() - 1);
L(i, 1, SZ() - 1) res[i - 1] = (ll) V[i] * i % MOD;
return res;
}
poly Ln() { // a[0] = 1
poly g = (V.Inv() * V.Deriv()).Integ();
return g.resize(SZ()), g;
}
poly Exp() { // a[0] = 0
poly f , res = {1} ;
for(int m = 1, pn; m < SZ(); m <<= 1) {
pn = min(m << 1, SZ()), f.resize(pn), res.resize(pn);
for(int i = 0; i < pn; i++) f[i] = v(i);
f -= res.Ln(), (f[0] += 1) %= MOD, res *= f, res.resize(pn);
}
return res.resize(SZ()), res;
}
poly qpow(int x) {
poly g(SZ()) , r = V; g[0] = 1 ;
for (;x;x >>= 1, r = r * r, r.resize(SZ()))
if (x & 1) g *= r , g.resize(SZ());
return g;
}
poly pow(int x, int rx = -1) { // x : the power % MOD; rx : the power % (MOD - 1)
if(rx == -1) rx = x;
int cnt = 0 , n = SZ();
while (V[cnt] == 0 && cnt < n) ++ cnt;
poly res = V >> cnt ;
int c = res[0], w = qmi(res[0]);
res = (res * w).Ln();
res = (res * x).Exp() ;
// cout << c << ' ' << rx << ' ' << ' ' << (ll)c * c % mod << ' ' << qmi(c , rx) << '\n' ;
res = res * qmi(c , rx);
// if ((ll)cnt * x > n) L(i, 0, n - 1) res[i] = 0;
// else if(cnt) {
// R(i, n - cnt * x - 1, 0) res[i + cnt * x] = res[i];
// L(i, 0, cnt * x - 1) res[i] = 0;
// }
res = res.shift((ll) cnt * x);
return res.resize(n) , res;
}
poly sqrt(int Rt = 1) { // a[0] = 1
poly res = {Rt} , f; int n = SZ() ;
for(int m = 1, pn; m < n; m <<= 1) {
pn = min(m << 1, n), f.resize(pn);
for(int i = 0; i < pn; i++) f[i] = v(i);
f += res * res, f.resize(pn), res.resize(pn), res = f * res.Inv(), res.resize(pn);
for(int i = 0; i < pn; i++) res[i] = (ll) res[i] * inv2 % MOD;
}
return res;
}
void Rev() { reverse(begin() , end()); }
void read() { for (int i = 0 ; i < (int)size() ; ++ i) { scanf("%d" , &V[i]); } }
void print() {
for (int i = 0 ; i < (int)size() || (puts("") , 0) ; ++ i) {
printf("%d " , V[i]) ;
}
}
#undef L
#undef R
#undef V
#undef SZ
#undef add
#undef dec
/*
65537 = 2^16 + 1 , G = 3
998244353 = 119 * 2^23 + 1 , G = 3
1004535809 = 479 * 2^21 + 1 , G = 3 (>1e9)
4179340454199820289 = 29 * 2^57 + 1 , G = 3 (>4e18)
*/
} ;
} using namespace POLY ;
inline int read () {
int x = 0, f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar ();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar ();
return x * f;
}
inline void write (int x) {
if (x < 0) x = -x, putchar ('-');
if (x >= 10) write (x / 10);
putchar (x % 10 + '0');
}
int n, m;
int a[70][70];
int f[2][40];
int ans[20005][40];
signed main () {
// freopen ("Para.in", "r", stdin);
// freopen ("1.out", "w", stdout);
n = read(), m = read();
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) a[i][j] = read();
for(ll fr = 0; fr < n; fr++) {
for(int i = 0; i < n; i++) f[0][i] = 0;
f[0][fr] = 1;
for(int i = 1; i <= m; i++) {
for(int j = 0; j < n; j++) f[i&1][j] = 0;
for(int j = 0; j < n; j++) for(int jj = 0; jj < n; jj++) f[i&1][j] += f[(i-1)&1][jj] * a[jj][j];
for(int j = 0; j < n; j++) f[i&1][j] %= mod, Add(ans[i][fr&j], f[i&1][j]);
}
}
for(int t = 1; t <= m; t++) for(int j = 0; (1 << j) < n; j++) for(int i = 0; i < n; i++) if(i >> j & 1) Add(ans[t][i ^ (1 << j)], ans[t][i]);
for(int i = 0; i < n; i++) {
poly g;
g.resize(m + 1);
g[0] = 1;
for(ll j = 1; j <= m; j++) g[j] = dec(0, ans[j][i]);
g = g.Inv();
for(ll j = 1; j <= m; j++) ans[j][i] = g[j];
}
for(int t = 1; t <= m; t++) for(int j = 0; (1 << j) < n; j++) for(int i = 0; i < n; i++) if(i >> j & 1) Dec(ans[t][i ^ (1 << j)], ans[t][i]);
int Ans = 0;
for(int i = 1; i <= m; i++) for(int j = 0; j < n; j++) Ans ^= ans[i][j];//, printf("?? %lld %lld %lld\n", i, j, ans[i][j]);
write(Ans), putchar('\n');
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 15
Accepted
Test #1:
score: 15
Accepted
time: 35ms
memory: 25620kb
input:
4 20 53674686 128460215 363694167 120271218 578912024 941426068 993595265 587468617 731649477 694107878 355552389 226535630 99325151 243772822 66420647 578481511
output:
588479706
result:
ok 1 number(s): "588479706"
Test #2:
score: 0
Accepted
time: 52ms
memory: 26360kb
input:
16 2000 315391385 70546806 184749201 528239002 741286788 146241680 165603053 41114445 572984599 227056916 305263327 980588113 367160763 155832001 212041467 995977944 491801679 569182307 859668874 327536237 778338213 762819679 11238663 728941676 307395331 62905643 662320291 849065607 550529700 282249...
output:
808196376
result:
ok 1 number(s): "808196376"
Test #3:
score: 0
Accepted
time: 18ms
memory: 25648kb
input:
8 100 681016308 947980964 71683807 222166989 633911431 994577572 50784856 298319502 40710797 455891003 503120116 503880841 988981490 34455718 806388934 589433205 327802386 18831411 193642391 916990555 375690304 69587142 72759137 6300182 494361154 892873815 205578968 352463324 154675154 301940078 523...
output:
625872426
result:
ok 1 number(s): "625872426"
Test #4:
score: 0
Accepted
time: 29ms
memory: 25780kb
input:
8 123 42141734 119453053 338794033 469472517 896943474 997318263 463090940 539752993 498132275 425586204 10331275 799002923 314685843 769975525 214134323 412654359 293167153 22579811 116636410 566990382 581484192 102682042 582694943 865977998 241553124 317584926 931548048 157765288 390446073 1855087...
output:
365380190
result:
ok 1 number(s): "365380190"
Test #5:
score: 0
Accepted
time: 17ms
memory: 26896kb
input:
16 5 157021390 252497011 468873864 892465002 392136945 867902344 634574021 416807938 771790989 545264217 666014055 30318904 997001481 92030626 658131204 234369349 43307846 497204529 460391395 44581806 107577365 871932521 470029953 149175762 27325626 819200707 411063821 891562850 191893364 790514504 ...
output:
85280541
result:
ok 1 number(s): "85280541"
Test #6:
score: 0
Accepted
time: 22ms
memory: 25924kb
input:
16 1 905581565 803883371 222387735 113243327 962846914 170270764 908119897 643672711 951948550 443390704 345262472 285268917 716661556 492805236 516049466 646586240 717205802 712046214 204789431 592343665 608834746 958679690 85385338 156417764 101326890 777140727 427162035 294565259 28802739 8285324...
output:
17305742
result:
ok 1 number(s): "17305742"
Test #7:
score: 0
Accepted
time: 45ms
memory: 27196kb
input:
16 1889 291592828 902768692 198687222 265645285 254788066 848949408 683064381 678157787 474168528 862103895 415053306 909831425 935938735 21752343 43155905 228914827 364068446 273045932 507519451 796711939 166902447 72879739 611394755 843648198 3964648 56114938 95672745 864116356 549815108 867090999...
output:
1000245565
result:
ok 1 number(s): "1000245565"
Test #8:
score: 0
Accepted
time: 42ms
memory: 26220kb
input:
16 1538 462407755 549251277 397837861 275994586 415476819 756356324 34904944 520197632 412094442 727727503 875452091 754767133 581156730 826224521 313192343 54965862 57572067 254043810 294905166 657621093 929264117 362049084 974381916 63383415 506826999 577114576 490305008 452699725 907812132 906157...
output:
141964726
result:
ok 1 number(s): "141964726"
Test #9:
score: 0
Accepted
time: 41ms
memory: 27360kb
input:
16 1234 344382827 816941878 819839652 144225696 371171347 966266104 884665589 169825013 765791827 40228758 652684236 893817865 725991829 758836428 252384190 124837650 871491255 654941544 640590098 175202200 748370570 752347597 100539461 38637304 762795606 419148404 537316998 134122726 29051987 94573...
output:
19530996
result:
ok 1 number(s): "19530996"
Test #10:
score: 0
Accepted
time: 36ms
memory: 26604kb
input:
16 1999 12949977 632196974 390885234 944145973 121838884 405002460 237547721 700781753 461518861 947091309 820524334 253274565 296734976 893264350 934506037 438497421 658309595 402128381 470963494 423098778 698061936 170098990 137416574 542917450 696307461 580493544 236741484 982061646 908169428 985...
output:
162533703
result:
ok 1 number(s): "162533703"
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #11:
score: 15
Accepted
time: 29ms
memory: 25676kb
input:
16 15 314601745 720760464 91785724 114888585 26836821 376536402 313090153 826230415 346734724 333924807 863460032 368340108 245372320 36392298 788713548 515760462 298875827 24627338 31898175 284907313 548032583 57534741 75385967 913412369 336300896 498149570 737179693 974479561 859718707 297256598 3...
output:
407303714
result:
ok 1 number(s): "407303714"
Test #12:
score: -15
Wrong Answer
time: 508ms
memory: 32228kb
input:
32 10000 761517608 533611175 252111851 40247028 679296910 479128630 648063750 922719043 15905318 450067426 673017960 429004044 149250097 455699505 781122302 247622532 8960746 139853274 715899414 341830718 10123661 958972771 98773785 523065821 813633478 25481882 318862349 244623710 119270736 59145995...
output:
714547454
result:
wrong answer 1st numbers differ - expected: '807722853', found: '714547454'
Subtask #3:
score: 0
Time Limit Exceeded
Test #21:
score: 35
Accepted
time: 440ms
memory: 36956kb
input:
16 20000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11 5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10 6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9 7 6 5 4 3 2 1 0 15 14 13 ...
output:
97601915
result:
ok 1 number(s): "97601915"
Test #22:
score: -35
Time Limit Exceeded
input:
64 20000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23 22 25 24 27 26 29 28 31 30 33 32 35 34 37 36 39 38...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%