#include "bits/stdc++.h"
using namespace std;
#define ar array
struct mtx{
vector<vector<int>> t;
int n;
mtx (int sz){
n = sz;
t.resize(n, vector<int>(n, 0));
}
mtx operator * (mtx& b){
mtx c(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
c.t[i][j] |= (t[i][k] & b.t[k][j]);
}
}
} return c;
}
};
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m, k; cin>>n>>m>>k;
vector<ar<int, 2>> a(k);
for(int i=0;i<k;i++){
cin>>a[i][0]>>a[i][1];
a[i][0]--, a[i][1]--;
}
if(n <= 7){
vector<mtx> pw(30, mtx(1 << n));
{
mtx dp(1 << n);
for(int i=0;i < (1 << n);i++){
for(int j=0;j < (1 << (n + n));j++){
int mask = 0, mm = i;
for(int l=0;l<n;l++){
if(mm >> l & 1) continue;
int v = (j >> (2 * l)) & 3;
if(v == 0){
if(l + 1 < n && !(mm >> (l + 1) & 1) && !(mask >> (l + 1) & 1)){
mm |= (1 << l);
mm |= (1 << (l + 1));
mask |= (1 << (l + 1));
}
}
if(v == 1){
if(l + 1 < n && !(mm >> (l + 1) & 1) && !(mask >> l & 1)){
mm |= (1 << l);
mm |= (1 << (l + 1));
mask |= (1 << l);
}
}
if(v == 2){
if(l && !(mask >> (l - 1) & 1) && !(mask >> l & 1)){
mm |= (1 << l);
mask |= (1 << l);
mask |= (1 << (l - 1));
}
}
if(v == 3){
if(l + 1 < n && !(mask >> (l + 1) & 1) && !(mask >> l & 1)){
mm |= (1 << l);
mask |= (1 << l);
mask |= (1 << (l + 1));
}
}
}
if(mm == (1 << n) - 1 && !dp.t[i][mask]){
//~ for(int j=0;j<n;j++) cout<<(i >> j & 1);
//~ cout<<endl;
//~ for(int j=0;j<n;j++) cout<<(mask >> j & 1);
//~ cout<<endl<<endl;
dp.t[i][mask] = 1;
}
}
}
pw[0] = dp;
for(int j=1;j<30;j++){
pw[j] = pw[j-1] * pw[j-1];
}
}
sort(a.begin(), a.end(), [&](auto& a, auto& b){
return (a[1] < b[1]);
});
vector<int> dp(1 << n);
dp[(1 << n) - 1] = 1;
int last = -1;
for(int i=0;i<k;){
int j = i, rev = 0;
while(j<k && a[j][1] == a[i][1]) rev |= (1 << a[j][0]), j++;
int d = a[i][1] - last;
for(int l=29;~l;l--){
if(d >> l & 1){
d ^= (1 << l);
vector<int> tp(1 << n);
for(int mask=0;mask < (1 << n);mask++){
for(int j=0;j < (1 << n);j++){
if(d){
tp[j] |= (dp[mask] & pw[l].t[mask][j]);
} else {
if(j & rev) continue;
tp[j | rev] |= (dp[mask] & pw[l].t[mask][j]);
}
}
}
swap(tp, dp);
}
} last = a[i][1], i = j;
}
int d = m - last - 1;
for(int l=29;~l;l--){
if(d >> l & 1){
vector<int> tp(1 << n);
for(int mask=0;mask < (1 << n);mask++){
for(int j=0;j < (1 << n);j++){
tp[j] |= dp[mask] & pw[l].t[mask][j];
}
}
swap(tp, dp);
}
}
if(dp[(1 << n) - 1]) cout<<"YES\n";
else cout<<"NO\n";
} else {
vector<vector<mtx>> pw(5, vector<mtx>(30, mtx(0)));
for(int t=2;t<7;t++){
int n = t + 1;
mtx dp(1 << n);
for(int i=0;i < (1 << n);i++){
for(int j=0;j < (1 << n);j++){
int mask = 0, mm = i;
for(int l=0;l<n;l++){
if(mm >> l & 1) continue;
if(l + 1 < n && !(mm >> (l + 1) & 1) && !(mask >> (l + (j >> l & 1)) & 1) &&
(j >> l & 1) == (j >> (l + 1) & 1)){
mm |= (1 << l);
mm |= (1 << (l + 1));
mask |= (1 << (l + (j >> l & 1)));
} else if((j >> l & 1) && l + 1 < n && !(mask >> (l + 1) & 1) && !(mask >> l & 1)){
mm |= (1 << l);
mask |= (1 << l);
mask |= (1 << (l + 1));
} else if(!(j >> l & 1) && l && !(mask >> (l - 1) & 1) && !(mask >> l & 1)){
mm |= (1 << l);
mask |= (1 << l);
mask |= (1 << (l - 1));
}
}
if(mm == (1 << n) - 1 && !dp.t[i][mask]){
//~ for(int j=0;j<n;j++) cout<<(i >> j & 1);
//~ cout<<endl;
//~ for(int j=0;j<n;j++) cout<<(mask >> j & 1);
//~ cout<<endl<<endl;
dp.t[i][mask] = 1;
}
}
}
pw[t - 2][0] = dp;
for(int j=1;j<30;j++){
pw[t - 2][j] = pw[t - 2][j-1] * pw[t - 2][j-1];
}
}
sort(a.begin(), a.end(), [&](auto& a, auto& b){
return (a[1] < b[1]);
});
vector<int> dp(1 << n);
auto solve = [&](int l, int r, vector<int>& t1, int& d, int& I, int rev){
int t = (r - l);
vector<int> d1(1 << t);
for(int mask=0;mask<(1 << n);mask++){
int m = (mask >> l) & ((1 << t) - 1);
d1[m] |= dp[mask];
} rev = (rev >> l) & ((1 << t) - 1);
for(int mask=0;mask < (1 << t);mask++){
for(int j=0;j < (1 << t);j++){
if(d){
t1[j] |= (d1[mask] & pw[t - 3][I].t[mask][j]);
} else {
if(rev & j) continue;
t1[rev | j] |= (d1[mask] & pw[t - 3][I].t[mask][j]);
}
}
}
};
dp[(1 << n) - 1] = 1;
int last = -1;
for(int i=0;i<k;){
int j = i, rev = 0;
while(j<k && a[j][1] == a[i][1]) rev |= (1 << a[j][0]), j++;
int d = a[i][1] - last;
for(int l=29;~l;l--){
if(d >> l & 1){
d ^= (1 << l);
vector<int> tp(1 << n);
for(int t=3;t<=7;t++){
int r = n - t;
if(r > 7) continue;
vector<int> t1(1 << t), t2(1 << r);
solve(0, t, t1, d, l, rev);
solve(t, n, t2, d, l, rev);
for(int mask=0;mask < (1 << n);mask++){
tp[mask] |= (t1[mask & ((1 << t) -1 )] & t2[mask >> t]);
}
}
swap(tp, dp);
}
} last = a[i][1], i = j;
}
int d = m - last - 1;
for(int l=29;~l;l--){
if(d >> l & 1){
vector<int> tp(1 << n);
for(int t=3;t<=7;t++){
int r = n - t;
if(r > 7) continue;
vector<int> t1(1 << t), t2(1 << r);
solve(0, t, t1, d, l, 0);
solve(t, n, t2, d, l, 0);
for(int mask=0;mask < (1 << n);mask++){
tp[mask] |= (t1[mask & ((1 << t) -1 )] & t2[mask >> t]);
}
}
swap(tp, dp);
}
}
if(dp[(1 << n) - 1]) cout<<"YES\n";
else cout<<"NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
96 ms |
2436 KB |
Output is correct |
8 |
Correct |
78 ms |
3132 KB |
Output is correct |
9 |
Correct |
79 ms |
3164 KB |
Output is correct |
10 |
Runtime error |
91 ms |
6312 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
2504 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
90 ms |
3172 KB |
Output is correct |
6 |
Correct |
94 ms |
3224 KB |
Output is correct |
7 |
Incorrect |
74 ms |
3236 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
13 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
9 ms |
496 KB |
Output is correct |
12 |
Correct |
27 ms |
896 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
468 KB |
Output is correct |
16 |
Correct |
22 ms |
916 KB |
Output is correct |
17 |
Correct |
6 ms |
468 KB |
Output is correct |
18 |
Correct |
6 ms |
468 KB |
Output is correct |
19 |
Correct |
21 ms |
912 KB |
Output is correct |
20 |
Correct |
15 ms |
892 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
25 ms |
892 KB |
Output is correct |
23 |
Correct |
2 ms |
372 KB |
Output is correct |
24 |
Correct |
9 ms |
492 KB |
Output is correct |
25 |
Correct |
24 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
5 ms |
500 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
6 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
468 KB |
Output is correct |
31 |
Correct |
6 ms |
492 KB |
Output is correct |
32 |
Correct |
23 ms |
912 KB |
Output is correct |
33 |
Correct |
27 ms |
916 KB |
Output is correct |
34 |
Correct |
16 ms |
852 KB |
Output is correct |
35 |
Correct |
16 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
468 KB |
Output is correct |
37 |
Correct |
15 ms |
916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
2512 KB |
Output is correct |
2 |
Correct |
116 ms |
2516 KB |
Output is correct |
3 |
Runtime error |
81 ms |
6376 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
96 ms |
2436 KB |
Output is correct |
8 |
Correct |
78 ms |
3132 KB |
Output is correct |
9 |
Correct |
79 ms |
3164 KB |
Output is correct |
10 |
Runtime error |
91 ms |
6312 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |