QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#879747 | #3086. Edge Subsets | Juefan | WA | 1ms | 3584kb | C++14 | 3.7kb | 2025-02-02 12:10:43 | 2025-02-02 12:10:44 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a,i##E=b;i<=i##E;i++)
#define UF(i,a,b) for(int i=a,i##E=b;i>=i##E;i--)
#define sz(x) int(x.size())
using namespace std;typedef long long ll;
#define vec vector
#define In(x) freopen(x".in","r",stdin)
#define Out(x) freopen(x".out","w",stdout)
#define File(x) (In(x),Out(x))
#define all(x) begin(x),end(x)
#define ran(x,l,r) begin(x)+l,begin(x)+(r+1)
template<typename T>
void operator+=(vec<T> &a,const T&b) {a.push_back(b);}
#define gc() getchar()
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char *p1,*p2,buf[1<<21];
int read() {
int s=0,w=0;char ch=gc();
while(ch<'0'||ch>'9') w|=(ch=='-'),ch=gc();
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=gc();
return w?-s:s;
} const int p=998244353;
using calc_t=int;
calc_t sol(int A,int B,vec<pair<int,int>> E) {
if(!sz(E)) return 1;
if(__gcd(A,B)>1) {
int g=__gcd(A,B);
A/=g,B/=g;
calc_t res=1;
F(i,0,g-1) {
vec<pair<int,int>> nE;
for(auto[u,v]:E) if(u%g==i) nE+=make_pair(u/g,v/g);
res=1ll*res*sol(A,B,nE)%p;
}
return res;
}
int n=0;
for(auto[u,v]:E) n=max(n,max(u,v));
++n;
int mx=max(n,(n+B-2)/B*B);//mx=mx*mx;
vec<int> pA(mx),pB(mx);
for(auto[u,v]:E) {
assert(v-u==A||v-u==B);
if(v-u==A) pA[v]=1;
else pB[v]=1;
}
// cerr<<"SOL:"<<n<<' '<<A<<' '<<B<<endl;
// for(auto[a,b]:E) cerr<<a<<' '<<b<<endl;
if(B<=sqrt(2*n)) {
// cerr<<"mode 1;"<<endl;
vec<calc_t> f(1<<B,0);
const int U=(1<<B)-1;
f[U]=1;
#define Z(x,y) (x+=y)%=p
F(i,0,n-1) {
vec<calc_t> g(1<<B,0);
if(pA[i]) {
F(j,0,U) if(!(j>>A-1&1)) Z(g[(j<<1|1|1<<A)&U],f[j]);
}
if(pB[i]) {
F(j,0,U) if(!(j>>B-1&1)) Z(g[(j<<1|1)&U],f[j]);
}
F(j,0,U) Z(g[(j<<1)&U],f[j]);
swap(f,g);
}
calc_t ans=0;
F(j,0,U) Z(ans,f[j]);
return ans;
}
// cerr<<"mode 2;"<<endl;
int G=(n+B-2)/B;
const int U=(1<<G)-1;
int ans=0;
F(C,0,(1<<G)-1) {
vec<calc_t> f(1<<G,0);
// if(C&1) continue;
// cerr<<"ST:"<<C<<endl;
f[C]=1;
int x=0;
F(_,1,B) {
x+=A;
if(x>=B) {
x-=B;
vec<calc_t> g(1<<G,0);
F(j,0,U) Z(g[(j<<1)&U],f[j]);
swap(f,g);
}
F(i,0,G-1) {
vec<calc_t> g(1<<G,0);
int k=x+i*B;
if(pA[k]) {
F(j,0,U) if(!(j>>i&1)) Z(g[j|1<<i],f[j]);
}
if(pB[k]) {
assert(i);
F(j,0,U) if(!(j>>i-1&1)) Z(g[j|1<<i-1|1<<i],f[j]);
}
F(j,0,U) Z(g[j&~(1<<i)],f[j]);
swap(f,g);
// cerr<<"AFT_K:"<<k<<' '<<endl;
// F(j,0,U) cerr<<"R:"<<j<<' '<<f[j]<<endl;
}
}
assert(!x);
Z(ans,f[C]);
} assert(ans>0);
return ans;
}
int main() {
// File("match");
// In("match_ex3");
int n,m,A,B;
n=read();m=read();A=read();B=read();
vec<pair<int,int>> E;
F(i,1,m) {
int u=read()-1,v=read()-1;
assert(v<n);
E+=make_pair(u,v);
}
cout<<sol(A,B,E)<<endl;
return 0;
}
/*
B<=20 好做,需要解决 B>20
按 B 分块,块数 <=10,块内按 mod A 同余分类。
考虑按照 (x+=A)%=B 的顺序处理所有 mod B 的同余类,此时类很多但是每类大小很小
复杂度 2^20.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3584kb
input:
50 97 31 32 5 37 15 46 2 33 15 46 6 37 14 46 1 32 2 34 5 36 1 32 5 37 3 35 11 42 12 44 2 33 1 33 10 42 3 35 12 43 14 46 18 49 2 34 1 33 12 43 15 47 13 45 12 44 11 42 9 41 1 32 7 39 3 35 4 35 11 43 11 42 13 45 2 34 12 43 6 38 18 50 3 35 4 36 13 45 17 49 12 43 3 34 15 46 3 34 1 32 11 42 4 35 2 34 10 4...
output:
28284459
result:
ok single line: '28284459'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
62 19 4 30 54 58 9 13 24 54 3 7 23 53 55 59 29 33 15 45 5 35 13 17 51 55 56 60 1 31 19 49 52 56 1 31 28 58 39 43 46 50
output:
69120
result:
ok single line: '69120'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
171 233 128 159 35 163 14 142 31 159 38 166 25 153 19 147 16 144 23 151 9 168 9 168 3 131 12 171 12 140 12 171 3 162 2 130 9 168 31 159 11 170 5 164 10 169 38 166 11 170 6 165 7 166 6 165 6 165 33 161 22 150 2 161 2 161 43 171 35 163 31 159 11 170 4 163 5 133 31 159 12 171 42 170 8 167 19 147 36 164...
output:
312199656
result:
ok single line: '312199656'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
67 61 41 56 4 60 7 48 10 66 2 58 13 54 11 67 1 57 26 67 26 67 1 57 18 59 5 61 24 65 25 66 4 60 3 59 7 48 7 48 11 67 8 64 21 62 11 52 16 57 8 64 11 52 9 65 19 60 12 53 3 59 4 45 5 61 24 65 17 58 5 46 8 64 1 57 4 60 8 64 1 57 10 66 6 47 10 66 6 62 7 48 2 43 6 47 2 58 9 65 24 65 5 61 16 57 10 66 4 60 2...
output:
6075000
result:
ok single line: '6075000'
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 3584kb
input:
28 38 16 24 3 27 2 18 4 28 9 25 4 28 3 27 4 20 2 26 6 22 3 19 12 28 2 26 4 28 1 25 9 25 1 25 3 19 4 28 5 21 1 25 2 26 2 26 2 26 8 24 6 22 1 25 10 26 12 28 2 18 8 24 10 26 3 27 4 28 12 28 2 26 3 27 1 25 3 27
output:
64
result:
wrong answer 1st lines differ - expected: '1800', found: '64'