QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#297792 | #5827. 异或图 | chenxinyang2006 | 20 | 956ms | 10948kb | C++14 | 9.6kb | 2024-01-05 10:23:55 | 2024-01-05 10:23:56 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template <class T>
void chkmax(T &x,T y){
if(x < y) x = y;
}
template <class T>
void chkmin(T &x,T y){
if(x > y) x = y;
}
inline int popcnt(int x){
return __builtin_popcount(x);
}
inline int ctz(int x){
return __builtin_ctz(x);
}
template <int P>
class mod_int
{
using Z = mod_int;
private:
static int mo(int x) { return x < 0 ? x + P : x; }
public:
int x;
int val() const { return x; }
mod_int() : x(0) {}
template <class T>
mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
bool operator==(const Z &rhs) const { return x == rhs.x; }
bool operator!=(const Z &rhs) const { return x != rhs.x; }
Z operator-() const { return Z(x ? P - x : 0); }
Z pow(long long k) const
{
Z res = 1, t = *this;
while (k)
{
if (k & 1)
res *= t;
if (k >>= 1)
t *= t;
}
return res;
}
Z &operator++()
{
x < P - 1 ? ++x : x = 0;
return *this;
}
Z &operator--()
{
x ? --x : x = P - 1;
return *this;
}
Z operator++(int)
{
Z ret = x;
x < P - 1 ? ++x : x = 0;
return ret;
}
Z operator--(int)
{
Z ret = x;
x ? --x : x = P - 1;
return ret;
}
Z inv() const { return pow(P - 2); }
Z &operator+=(const Z &rhs)
{
(x += rhs.x) >= P && (x -= P);
return *this;
}
Z &operator-=(const Z &rhs)
{
(x -= rhs.x) < 0 && (x += P);
return *this;
}
Z operator-()
{
return -x;
}
Z &operator*=(const Z &rhs)
{
x = 1ULL * x * rhs.x % P;
return *this;
}
Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o) \
friend T operator o(const Z &lhs, const Z &rhs) \
{ \
Z res = lhs; \
return res o## = rhs; \
}
setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
friend istream& operator>>(istream& is, mod_int& x)
{
long long tmp;
is >> tmp;
x = tmp;
return is;
}
friend ostream& operator<<(ostream& os, const mod_int& x)
{
os << x.val();
return os;
}
};
using Z = mod_int<mod>;
Z power(Z p,ll k){
Z ans = 1;
while(k){
if(k % 2 == 1) ans *= p;
p *= p;
k /= 2;
}
return ans;
}
int n,m;
ll inst;
ll a[25];
Z fact[25],ifac[25],inv[25];
int ord[25],rk[25];
bool cmp(int x,int y){
return a[x] > a[y];
}
int msk[25],_sum[1 << 15];
Z val[3][16][1 << 15],_ret[1 << 15];
void FWT(Z *f){
rep(i,0,n - 1){
rep(S,0,(1 << n) - 1) if((S >> i) & 1) f[S] += f[S - (1 << i)];
}
}
void iFWT(Z *f){
rep(i,0,n - 1){
rep(S,0,(1 << n) - 1) if((S >> i) & 1) f[S] -= f[S - (1 << i)];
}
}
Z tmp[3][25];
void ln(Z *f,Z *g){//f = ln g
fill(f,f + n + 1,0);
rep(i,0,n - 1){
f[i + 1] = (i + 1) * g[i + 1];
rep(j,0,i - 1) f[i + 1] -= (j + 1) * f[j + 1] * g[i - j];
f[i + 1] *= inv[i + 1];
}
}
void Exp(Z *g,Z *f){//g = e^f
fill(g,g + n + 1,0);
g[0] = 1;
rep(i,0,n - 1){
rep(j,0,i) g[i + 1] += (j + 1) * f[j + 1] * g[i - j];
g[i + 1] *= inv[i + 1];
}
}
void conv(Z *f,Z *g,Z *res,int k){//size 是 2^k
rep(i,0,k){
fill(val[0][i],val[0][i] + (1 << k),0);
fill(val[1][i],val[1][i] + (1 << k),0);
}
rep(S,0,(1 << k) - 1){
val[0][popcnt(S)][S] += f[S];
val[1][popcnt(S)][S] += g[S];
}
rep(i,0,k){
FWT(val[0][i]);
FWT(val[1][i]);
}
rep(S,0,(1 << k) - 1){
rep(i,0,k){
tmp[0][i] = val[0][i][S];
tmp[1][i] = val[1][i][S];
tmp[2][i] = 0;
}
rep(i,0,k){
rep(j,0,k - i) tmp[2][i + j] += tmp[0][i] * tmp[1][j];
}
rep(i,0,k) val[2][i][S] = tmp[2][i];
}
rep(i,0,k) iFWT(val[2][i]);
rep(S,0,(1 << k) - 1) res[S] = val[2][popcnt(S)][S];
}
Z dp[1 << 15],ndp[1 << 15],A[1 << 15],B[1 << 15],C[1 << 15];
Z _dp[2][2],_ndp[2][2],b[25];
Z calc(int S){
// printf("solve S=%d\n",S);
Z ans = 0;
int qwq;
per(p,59,0){
_dp[0][0] = 1;_dp[0][1] = _dp[1][0] = _dp[1][1] = 0;
rep(i,0,n - 1) b[i] = (a[i] & ((1ll << p) - 1)) + 1;
qwq = 0;
rep(i,0,n - 1){
if(!((S >> i) & 1)) continue;
if((a[i] >> p) & 1){
rep(v,0,1){
_ndp[v][1] += _dp[v][1] * (1ll << p) + _dp[v][0];
_ndp[v ^ 1][1] += _dp[v][1] * b[i];
_ndp[v ^ 1][0] += _dp[v][0] * b[i];
}
qwq ^= 1;
}else{
rep(v,0,1){
_ndp[v][1] += _dp[v][1] * b[i];
_ndp[v][0] += _dp[v][0] * b[i];
}
}
rep(v,0,1){
rep(o,0,1){
_dp[v][o] = _ndp[v][o];
_ndp[v][o] = 0;
}
}
}
ans += _dp[(inst >> p) & 1][1];
if(qwq != ((inst >> p) & 1)) break;
if(!p) ans++;
}
// printf("%d\n",ans.val());
return ans;
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
scanf("%d%d%lld",&n,&m,&inst);
fact[0] = 1;
rep(i,1,n) fact[i] = fact[i - 1] * i;
ifac[n] = Z(1) / fact[n];
per(i,n - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
rep(i,1,n) inv[i] = ifac[i] * fact[i - 1];
rep(u,0,n - 1){
scanf("%lld",&a[u]);
ord[u] = u;
}
sort(ord,ord + n,cmp);
rep(i,0,n - 1) rk[ord[i]] = i;
sort(a,a + n);reverse(a,a + n);
rep(i,1,m){
int u,v;
scanf("%d%d",&u,&v);
u = rk[u - 1];v = rk[v - 1];
msk[u] |= 1 << v;msk[v] |= 1 << u;
}
rep(S,1,(1 << n) - 1){
int u = ctz(S);
_sum[S] = _sum[S - (1 << u)] + popcnt(msk[u] & S);
}
// rep(S,0,(1 << n) - 1) printf("S=%d:%d\n",S,_sum[S]);
rep(S,0,(1 << n) - 1) if(!_sum[S]) val[0][popcnt(S)][S]++;
rep(i,0,n) FWT(val[0][i]);
rep(S,0,(1 << n) - 1){
// printf("S=%d\n",S);
rep(i,0,n) tmp[1][i] = val[0][i][S];
// rep(i,0,n) printf("%d ",tmp[1][i].val());
// printf("\n");
ln(tmp[0],tmp[1]);
// rep(i,0,n) printf("%d ",tmp[0][i].val());
// printf("\n");
rep(i,0,n) val[1][i][S] = tmp[0][i];
}
rep(i,0,n) iFWT(val[1][i]);
rep(S,0,(1 << n) - 1) _ret[S] = val[1][popcnt(S)][S];
// printf("ret:\n");
// rep(S,0,(1 << n) - 1) printf("S=%d:%d\n",S,_ret[S].val());
rep(i,0,n) fill(val[0][i],val[0][i] + (1 << n),0);
rep(S,1,(1 << n) - 1) if(!__builtin_parity(S)) val[0][popcnt(S)][S] += _ret[S] * (a[__lg(S)] + 1);
// rep(S,0,(1 << n) - 1) printf("S=%d:%d\n",S,val[0][popcnt(S)][S].val());
rep(i,0,n) FWT(val[0][i]);
rep(S,0,(1 << n) - 1){
// printf("%d:\n",S);
rep(i,0,n) tmp[1][i] = val[0][i][S];
Exp(tmp[0],tmp[1]);
/* rep(i,0,n) printf("%d ",tmp[1][i].val());
printf("\n");
rep(i,0,n) printf("%d ",tmp[0][i].val());
printf("\n");*/
rep(i,0,n) val[1][i][S] = tmp[0][i];
}
rep(i,0,n) iFWT(val[1][i]);
rep(S,0,(1 << n) - 1) dp[(1 << n) - 1 - S] = val[1][popcnt(S)][S];
dp[(1 << n) - 1] = 1;
// rep(S,0,(1 << n) - 1) printf("%d ",dp[S].val());
// printf("\n");
per(pos,n - 1,0){
// printf("pos=%d\n",pos);
int s = pos + 1;
rep(P,0,(1 << (n - pos - 1)) - 1){
fill(A,A + (1 << s),0);fill(B,B + (1 << s),0);fill(C,C + (1 << s),0);
rep(S,0,(1 << s) - 1){
if((S >> pos) & 1){
A[(1 << s) - 1 - S] += dp[(P << s) | S];
if(__builtin_parity(S)) B[S] += _ret[S];
}
}
/* printf("P=%d\n",P);
rep(S,0,(1 << s) - 1) printf("%d ",A[S].val());
printf("\n");
rep(S,0,(1 << s) - 1) printf("%d ",B[S].val());
printf("\n");*/
conv(A,B,C,s);
// rep(S,0,(1 << s) - 1) printf("%d ",C[S].val());
// printf("\n");
rep(S,0,(1 << s) - 1){
if((S >> pos) & 1) ndp[(P << s) + (1 << (pos + 1)) - 1 - S + (1 << pos)] += C[S];
else ndp[(P << s) | S] += dp[(P << s) | S];
}
}
rep(S,0,(1 << n) - 1){
dp[S] = ndp[S];
ndp[S] = 0;
// printf("%d ",dp[S].val());
}
// printf("\n");
}
/* printf("final:\n");
rep(S,0,(1 << n) - 1){
rep(i,0,n - 1) printf("%d",(S >> i) & 1);
printf(" %d\n",dp[S].val());
}
printf("\n");*/
Z ans = 0;
rep(S,0,(1 << n) - 1) ans += dp[S] * calc(S);
printf("%d\n",ans.val());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 0ms
memory: 10916kb
input:
4 6 2 7 11 14 0 1 2 1 3 2 3 2 4 4 1 4 3
output:
44
result:
ok 1 number(s): "44"
Test #2:
score: 0
Accepted
time: 3ms
memory: 10640kb
input:
4 4 6 12 14 14 5 4 2 1 4 3 2 1 2
output:
798
result:
ok 1 number(s): "798"
Test #3:
score: 0
Accepted
time: 0ms
memory: 10924kb
input:
3 3 2 10 4 11 2 1 3 2 1 3
output:
33
result:
ok 1 number(s): "33"
Test #4:
score: 0
Accepted
time: 0ms
memory: 10684kb
input:
4 0 4 9 8 5 2
output:
148
result:
ok 1 number(s): "148"
Test #5:
score: 0
Accepted
time: 2ms
memory: 10688kb
input:
5 6 14 12 15 13 13 12 3 1 2 4 2 5 2 1 5 3 4 5
output:
21337
result:
ok 1 number(s): "21337"
Test #6:
score: 0
Accepted
time: 2ms
memory: 10680kb
input:
4 5 5 5 2 4 13 2 1 3 4 1 4 4 2 3 2
output:
42
result:
ok 1 number(s): "42"
Test #7:
score: 0
Accepted
time: 2ms
memory: 10692kb
input:
4 4 3 13 7 8 12 4 1 3 1 2 4 4 3
output:
552
result:
ok 1 number(s): "552"
Test #8:
score: 0
Accepted
time: 1ms
memory: 10680kb
input:
4 2 12 1 12 4 11 2 1 3 1
output:
70
result:
ok 1 number(s): "70"
Test #9:
score: 0
Accepted
time: 0ms
memory: 10920kb
input:
5 5 6 10 7 8 2 13 1 5 1 3 2 1 4 3 5 3
output:
1231
result:
ok 1 number(s): "1231"
Test #10:
score: 0
Accepted
time: 2ms
memory: 10640kb
input:
5 7 9 6 7 13 15 12 1 3 5 3 5 2 4 5 4 3 4 1 3 2
output:
6223
result:
ok 1 number(s): "6223"
Test #11:
score: 0
Accepted
time: 0ms
memory: 10948kb
input:
3 0 3 15 7 12
output:
104
result:
ok 1 number(s): "104"
Test #12:
score: 0
Accepted
time: 2ms
memory: 10684kb
input:
3 2 9 10 6 5 1 2 1 3
output:
17
result:
ok 1 number(s): "17"
Test #13:
score: 0
Accepted
time: 0ms
memory: 10640kb
input:
5 5 11 7 9 15 9 9 5 4 5 1 5 2 1 3 3 4
output:
5224
result:
ok 1 number(s): "5224"
Test #14:
score: 0
Accepted
time: 2ms
memory: 10764kb
input:
5 0 12 9 8 14 11 2
output:
3006
result:
ok 1 number(s): "3006"
Test #15:
score: 0
Accepted
time: 2ms
memory: 10708kb
input:
3 1 1 6 10 4 3 1
output:
30
result:
ok 1 number(s): "30"
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #16:
score: 50
Accepted
time: 10ms
memory: 10708kb
input:
9 27 705410105529944560 929827299070190972 733413770730537329 473007347105710981 190062421504120247 918561152768663129 196040702922254016 981530663192980241 203295856357272834 337150461958783092 2 8 7 9 8 9 2 3 9 2 2 7 9 5 9 4 4 8 1 7 6 3 6 1 4 1 6 5 2 4 2 1 9 3 9 6 7 3 7 5 5 2 4 5 2 6 3 1 3 8 4 3 8 6
output:
5392583
result:
ok 1 number(s): "5392583"
Test #17:
score: 0
Accepted
time: 10ms
memory: 10680kb
input:
9 7 788762650337246371 605340092851479114 625896945107761227 361131331380167081 572133549445050458 929899192003968010 340514051531987427 690728985364969400 268762741220048006 818120252827139346 5 8 9 6 6 1 1 9 9 8 5 1 4 5
output:
35237078
result:
ok 1 number(s): "35237078"
Test #18:
score: 0
Accepted
time: 2ms
memory: 10712kb
input:
7 8 968166787047166534 945734997493219809 465616677643913237 530128109571749460 717120283671096308 118646732725835921 510958884109370001 797022604947155276 5 2 4 7 1 2 6 5 4 2 4 6 1 6 6 3
output:
133871438
result:
ok 1 number(s): "133871438"
Test #19:
score: 0
Accepted
time: 956ms
memory: 10772kb
input:
12 21 341964498832651322 422448536649714733 488538974423366199 893293448611252565 879334133559023407 13516625885288091 43377983230712374 263189254162337644 474056776923289355 540904774976211471 103364876621830299 515157013276720499 213857038587555252 12 9 8 3 1 9 1 7 3 1 8 11 11 10 6 10 6 1 10 2 7 9...
output:
296076062
result:
ok 1 number(s): "296076062"
Test #20:
score: 0
Accepted
time: 218ms
memory: 10712kb
input:
11 42 215284372701527433 670445786006000260 969876209382224733 248721347029697734 375741447316879814 840434941395805804 187091598433077755 126574401069916039 764298539206353847 750906714570719526 387387869969339518 713140316419888823 1 10 2 5 1 7 4 11 3 11 2 7 4 5 9 5 1 6 3 4 10 9 11 9 3 7 2 1 8 11 ...
output:
861118590
result:
ok 1 number(s): "861118590"
Test #21:
score: 0
Accepted
time: 3ms
memory: 10756kb
input:
7 20 619868500075052677 653541655679358091 619279335581334164 74945438024390700 772996180610853550 636253173293891586 125935970032544337 454311587629767538 7 3 4 5 6 7 2 7 4 2 5 3 4 6 2 6 7 4 5 7 2 5 6 3 5 1 2 3 3 4 1 7 2 1 1 3 5 6 4 1
output:
396474896
result:
ok 1 number(s): "396474896"
Test #22:
score: -50
Time Limit Exceeded
input:
13 1 655058659126783551 220930961455414900 363602338013759573 443737606888655227 137555247528320912 492558319379424931 930253239754276705 727679308735300884 787033056632957722 29595553176095069 586820353385061840 342786039873677428 141912073483259823 800159879032310691 4 9
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #47:
score: 0
Time Limit Exceeded
input:
14 0 731833687287532167 157552918690640051 900457311668526045 111217720157614956 84140984111060473 814012186135880499 784848789620248379 958953377683017264 105083874298571687 104921429970878846 44983041675142735 871013110538758030 686733907990421995 98063590462078176 495161493555059993
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%