#include <iostream>
#include <vector>
#define mod 1000003
using namespace std;
struct DisjointSetUnion {
int n;
int com;
vector<int> fa;
void init(int _n) {
n = _n;
com = n;
fa.resize(n);
for (int i=0; i<n; i++) fa[i] = i;
}
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void merge(int x, int y) {
if (find(x) == find(y)) return;
com--;
fa[find(y)] = find(x);
}
};
inline long long invmod(long long x) {
long long ans = 1;
for (int i=mod-2; i; i>>=1) {
if (i&1) ans = ans * x % mod;
x = x * x % mod;
}
return ans;
}
DisjointSetUnion DSU;
long long fac[1000001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n-k+1);
vector<int> ans(n, -1);
for (int i=0; i<n-k+1; i++) cin >> a[i];
DSU.init(n+2);
for (int i=0; i<n-k; i++) {
int diff = a[i+1] - a[i];
if (diff == 1) {
DSU.merge(n, i);
DSU.merge(n+1, i+k);
}else if (diff == -1) {
DSU.merge(n+1, i);
DSU.merge(n, i+k);
}else if (diff == 0) {
DSU.merge(i, i+k);
}else{
cout << "0\n";
return 0;
}
}
if (DSU.find(n) == DSU.find(n+1)) {
cout << "0\n";
return 0;
}
for (int i=0; i<n; i++) {
if (DSU.find(i) == n) ans[i] = 0;
if (DSU.find(i)== n+1) ans[i] = 1;
}
fac[0] = 1;
for (int i=1; i<=k; i++) fac[i] = fac[i-1] * i % mod;
int space = 0, one = 0;
for (int i=0; i<k; i++) {
if (ans[i] == -1) space++;
if (ans[i] == 1) one++;
}
if (one > a[0]) {
cout << "0\n";
return 0;
}
int zero = a[0] - one;
cout << fac[space] * invmod(fac[zero]) % mod * invmod(fac[space-zero]) % mod << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
50 ms |
12200 KB |
Output is correct |
36 |
Correct |
55 ms |
12116 KB |
Output is correct |
37 |
Correct |
54 ms |
12368 KB |
Output is correct |
38 |
Correct |
50 ms |
12112 KB |
Output is correct |
39 |
Correct |
43 ms |
12116 KB |
Output is correct |
40 |
Correct |
50 ms |
12116 KB |
Output is correct |
41 |
Correct |
63 ms |
12368 KB |
Output is correct |
42 |
Correct |
51 ms |
12116 KB |
Output is correct |
43 |
Correct |
48 ms |
12140 KB |
Output is correct |
44 |
Correct |
53 ms |
12120 KB |
Output is correct |
45 |
Correct |
50 ms |
12116 KB |
Output is correct |
46 |
Correct |
48 ms |
12124 KB |
Output is correct |
47 |
Correct |
58 ms |
12372 KB |
Output is correct |
48 |
Correct |
51 ms |
12116 KB |
Output is correct |
49 |
Correct |
51 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
50 ms |
12200 KB |
Output is correct |
36 |
Correct |
55 ms |
12116 KB |
Output is correct |
37 |
Correct |
54 ms |
12368 KB |
Output is correct |
38 |
Correct |
50 ms |
12112 KB |
Output is correct |
39 |
Correct |
43 ms |
12116 KB |
Output is correct |
40 |
Correct |
50 ms |
12116 KB |
Output is correct |
41 |
Correct |
63 ms |
12368 KB |
Output is correct |
42 |
Correct |
51 ms |
12116 KB |
Output is correct |
43 |
Correct |
48 ms |
12140 KB |
Output is correct |
44 |
Correct |
53 ms |
12120 KB |
Output is correct |
45 |
Correct |
50 ms |
12116 KB |
Output is correct |
46 |
Correct |
48 ms |
12124 KB |
Output is correct |
47 |
Correct |
58 ms |
12372 KB |
Output is correct |
48 |
Correct |
51 ms |
12116 KB |
Output is correct |
49 |
Correct |
51 ms |
12112 KB |
Output is correct |
50 |
Correct |
62 ms |
12128 KB |
Output is correct |
51 |
Correct |
64 ms |
12112 KB |
Output is correct |
52 |
Correct |
62 ms |
12112 KB |
Output is correct |
53 |
Correct |
102 ms |
12104 KB |
Output is correct |
54 |
Correct |
70 ms |
12108 KB |
Output is correct |
55 |
Correct |
67 ms |
12180 KB |
Output is correct |
56 |
Correct |
64 ms |
12184 KB |
Output is correct |
57 |
Correct |
82 ms |
12108 KB |
Output is correct |
58 |
Correct |
80 ms |
12108 KB |
Output is correct |
59 |
Correct |
66 ms |
12112 KB |
Output is correct |
60 |
Correct |
61 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
50 ms |
12200 KB |
Output is correct |
36 |
Correct |
55 ms |
12116 KB |
Output is correct |
37 |
Correct |
54 ms |
12368 KB |
Output is correct |
38 |
Correct |
50 ms |
12112 KB |
Output is correct |
39 |
Correct |
43 ms |
12116 KB |
Output is correct |
40 |
Correct |
50 ms |
12116 KB |
Output is correct |
41 |
Correct |
63 ms |
12368 KB |
Output is correct |
42 |
Correct |
51 ms |
12116 KB |
Output is correct |
43 |
Correct |
48 ms |
12140 KB |
Output is correct |
44 |
Correct |
53 ms |
12120 KB |
Output is correct |
45 |
Correct |
50 ms |
12116 KB |
Output is correct |
46 |
Correct |
48 ms |
12124 KB |
Output is correct |
47 |
Correct |
58 ms |
12372 KB |
Output is correct |
48 |
Correct |
51 ms |
12116 KB |
Output is correct |
49 |
Correct |
51 ms |
12112 KB |
Output is correct |
50 |
Correct |
62 ms |
12128 KB |
Output is correct |
51 |
Correct |
64 ms |
12112 KB |
Output is correct |
52 |
Correct |
62 ms |
12112 KB |
Output is correct |
53 |
Correct |
102 ms |
12104 KB |
Output is correct |
54 |
Correct |
70 ms |
12108 KB |
Output is correct |
55 |
Correct |
67 ms |
12180 KB |
Output is correct |
56 |
Correct |
64 ms |
12184 KB |
Output is correct |
57 |
Correct |
82 ms |
12108 KB |
Output is correct |
58 |
Correct |
80 ms |
12108 KB |
Output is correct |
59 |
Correct |
66 ms |
12112 KB |
Output is correct |
60 |
Correct |
61 ms |
12116 KB |
Output is correct |
61 |
Correct |
12 ms |
15964 KB |
Output is correct |
62 |
Correct |
13 ms |
16124 KB |
Output is correct |
63 |
Correct |
15 ms |
15960 KB |
Output is correct |
64 |
Correct |
14 ms |
15960 KB |
Output is correct |
65 |
Correct |
62 ms |
14420 KB |
Output is correct |
66 |
Correct |
35 ms |
15952 KB |
Output is correct |
67 |
Correct |
66 ms |
13392 KB |
Output is correct |
68 |
Correct |
17 ms |
15704 KB |
Output is correct |
69 |
Correct |
15 ms |
15964 KB |
Output is correct |
70 |
Correct |
13 ms |
15964 KB |
Output is correct |
71 |
Correct |
15 ms |
15964 KB |
Output is correct |