#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int MOD = 1e6+3;
struct dsu{
vector<int> arr;
int n;
void init(int _n){
n = _n;
arr.resize(n);
for(int i=0;i<n;i++) arr[i]=i;
}
int query(int a){
if(arr[a]==a) return a;
arr[a] = query(arr[a]);
return arr[a];
}
void join(int a,int b){
a = query(a);
b = query(b);
if(a!=b) arr[a]=b;
}
};
vector<int> fac;
vector<int> invfac;
int C(int n,int k){
ll r = 1LL*((1LL*fac[n]*invfac[k])%MOD)*invfac[n-k]%MOD;
return r;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,k;cin>>n>>k;
fac.resize(k+1);
invfac.resize(k+1);
fac[0]=1;
for(int i=1;i<=k;i++) fac[i] = (1LL*fac[i-1]*i)%MOD;
ll g = fac[k];
ll r = 1;
ll p = MOD-2;
while(p){
if(p&1) r = (r*g)%MOD;
g = (g*g)%MOD;
p>>=1;
}
invfac[k]=r;
for(int i=k;i>=1;i--){
invfac[i-1] = (1LL*invfac[i]*i)%MOD;
}
vector<int> arr(n-k+1);
for(int i=0;i<n-k+1;i++){
cin>>arr[i];
}
dsu d;
d.init(n);
vector<int> hasto(n,-1);
for(int i=0;i<n-k;i++){
if(arr[i]==arr[i+1]){
d.join(i,i+k);
}else if(arr[i]+1==arr[i+1]){
hasto[i]=0;
hasto[i+k]=1;
}else{
hasto[i]=1;
hasto[i+k]=0;
}
}
for(int i=0;i<n;i++) {
int a = d.query(i);
if(hasto[i]!=-1) hasto[a]=hasto[i];
}
for(int i=0;i<n;i++) {
hasto[i] = hasto[d.query(i)];
}
map<int,int> stuff;
int cnt = 0;
for(int i=0;i<k;i++){
if(hasto[i]==1) cnt++;
if(hasto[i]==-1){
stuff[d.query(i)]++;
}
}
int K = arr[0]-cnt;
map<int,int> btw;
for(auto i : stuff) btw[i.second]++;
vector<pair<int,int> > w;
for(auto i : btw) w.push_back(i);
w.push_back({0,0});
sort(w.begin(),w.end());
vector<vector<int>> dp(w.size(),vector<int>(K+1,0));
dp[0][0]=1;
for(int i=1;i<w.size();i++){
for(int j=0;j<=K;j++){
for(int h=0;h<=w[i].second;h++){
if(j<h*w[i].first) break;
dp[i][j] = (dp[i][j]+((1LL*C(w[i].second,h)*dp[i-1][j-h*w[i].first])%MOD))%MOD;
}
}
}
cout<<dp[w.size()-1][K]<<"\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:97:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=1;i<w.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
424 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
424 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
50 ms |
12628 KB |
Output is correct |
36 |
Correct |
57 ms |
12624 KB |
Output is correct |
37 |
Correct |
55 ms |
12628 KB |
Output is correct |
38 |
Correct |
55 ms |
12700 KB |
Output is correct |
39 |
Correct |
48 ms |
12624 KB |
Output is correct |
40 |
Correct |
53 ms |
12632 KB |
Output is correct |
41 |
Correct |
57 ms |
12852 KB |
Output is correct |
42 |
Correct |
56 ms |
13144 KB |
Output is correct |
43 |
Correct |
48 ms |
12880 KB |
Output is correct |
44 |
Correct |
57 ms |
24988 KB |
Output is correct |
45 |
Correct |
55 ms |
25172 KB |
Output is correct |
46 |
Correct |
55 ms |
25068 KB |
Output is correct |
47 |
Correct |
52 ms |
18516 KB |
Output is correct |
48 |
Correct |
54 ms |
12808 KB |
Output is correct |
49 |
Correct |
71 ms |
12804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
424 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
50 ms |
12628 KB |
Output is correct |
36 |
Correct |
57 ms |
12624 KB |
Output is correct |
37 |
Correct |
55 ms |
12628 KB |
Output is correct |
38 |
Correct |
55 ms |
12700 KB |
Output is correct |
39 |
Correct |
48 ms |
12624 KB |
Output is correct |
40 |
Correct |
53 ms |
12632 KB |
Output is correct |
41 |
Correct |
57 ms |
12852 KB |
Output is correct |
42 |
Correct |
56 ms |
13144 KB |
Output is correct |
43 |
Correct |
48 ms |
12880 KB |
Output is correct |
44 |
Correct |
57 ms |
24988 KB |
Output is correct |
45 |
Correct |
55 ms |
25172 KB |
Output is correct |
46 |
Correct |
55 ms |
25068 KB |
Output is correct |
47 |
Correct |
52 ms |
18516 KB |
Output is correct |
48 |
Correct |
54 ms |
12808 KB |
Output is correct |
49 |
Correct |
71 ms |
12804 KB |
Output is correct |
50 |
Correct |
76 ms |
12116 KB |
Output is correct |
51 |
Correct |
91 ms |
12112 KB |
Output is correct |
52 |
Correct |
74 ms |
12304 KB |
Output is correct |
53 |
Correct |
72 ms |
12112 KB |
Output is correct |
54 |
Correct |
97 ms |
12208 KB |
Output is correct |
55 |
Correct |
117 ms |
12312 KB |
Output is correct |
56 |
Correct |
80 ms |
12308 KB |
Output is correct |
57 |
Correct |
61 ms |
12068 KB |
Output is correct |
58 |
Correct |
103 ms |
12380 KB |
Output is correct |
59 |
Correct |
87 ms |
12116 KB |
Output is correct |
60 |
Correct |
73 ms |
12220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
388 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
424 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
50 ms |
12628 KB |
Output is correct |
36 |
Correct |
57 ms |
12624 KB |
Output is correct |
37 |
Correct |
55 ms |
12628 KB |
Output is correct |
38 |
Correct |
55 ms |
12700 KB |
Output is correct |
39 |
Correct |
48 ms |
12624 KB |
Output is correct |
40 |
Correct |
53 ms |
12632 KB |
Output is correct |
41 |
Correct |
57 ms |
12852 KB |
Output is correct |
42 |
Correct |
56 ms |
13144 KB |
Output is correct |
43 |
Correct |
48 ms |
12880 KB |
Output is correct |
44 |
Correct |
57 ms |
24988 KB |
Output is correct |
45 |
Correct |
55 ms |
25172 KB |
Output is correct |
46 |
Correct |
55 ms |
25068 KB |
Output is correct |
47 |
Correct |
52 ms |
18516 KB |
Output is correct |
48 |
Correct |
54 ms |
12808 KB |
Output is correct |
49 |
Correct |
71 ms |
12804 KB |
Output is correct |
50 |
Correct |
76 ms |
12116 KB |
Output is correct |
51 |
Correct |
91 ms |
12112 KB |
Output is correct |
52 |
Correct |
74 ms |
12304 KB |
Output is correct |
53 |
Correct |
72 ms |
12112 KB |
Output is correct |
54 |
Correct |
97 ms |
12208 KB |
Output is correct |
55 |
Correct |
117 ms |
12312 KB |
Output is correct |
56 |
Correct |
80 ms |
12308 KB |
Output is correct |
57 |
Correct |
61 ms |
12068 KB |
Output is correct |
58 |
Correct |
103 ms |
12380 KB |
Output is correct |
59 |
Correct |
87 ms |
12116 KB |
Output is correct |
60 |
Correct |
73 ms |
12220 KB |
Output is correct |
61 |
Execution timed out |
1031 ms |
68944 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |