#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e6+3;
const int inf = 1e18;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
vector<int>fact;
int cho(int n,int r){
if(r > n or r < 0){
return 0;
}
// return fact[n]/(fact[r]*fact[n-r]);
return di(fact[n],mul(fact[n-r],fact[r],M),M);
}
void solve(int testcase_number){
cin>>n>>k;
input(int,l,n-k+1);
// print(l);
// vector<int>l;
// for(int i = 0;i<n-k+1;i++)l.pb(i%5);
// print(l);
// int ans1 = 0;
// for(int i =0 ;i<(1<<k);i++){
// if(__builtin_popcount(i) != l[0])continue;
//
// vector<int>a(n+1,0);
// for(int j = 0;j<k;j++){
// a[j] = (((1<<j)&i)!=0);
// }
// int p1 = 0;
// int p2 = k;
// int j = 1;
// int sum = l[0];
// int f =1 ;
// while(p2<=n and j<(int)l.size()){
// if(l[j] == sum-a[p1]+1){
// a[p2] = 1;
// p2++;
// p1++;
// sum = l[j];
// j++;
// }
// else if(l[j] == sum-a[p1]){
// a[p2] = 0;
// p2++;
// p1++;
// sum = l[j];
// j++;
// }
// else{
// f = 0;
//
// break;
// }
//
// }
// if(f){
// ans1++;
// }
// // print(a);
//
// }
// cout<<ans1<<endl;
vector<int>a(n+1,-1);
for(int i = 0;i+1<(int)l.size();i++){
if(l[i] > l[i+1]){
a[i] = 1;
a[i+k] = 0;
// int t = i+k;
// int j = 1;
// while(t<l.size() and l[i+j] ==l[t]){
// a[i+k+k*j] = 0;
// t+=k;
// j++;
// }
}
else if(l[i]<l[i+1]){
a[i] = 0;
a[i+k] = 1;
// int t = i+k;
// int j = 1;
// while(t<l.size() and l[i+j] ==l[t]){
// a[i+k+k*j] = 1;
// t+=k;
// j++;
// }
}
}
vector<int>cnt,cnt1;
cnt1 = {0};
cnt = cnt1;
for(int i:a){
cnt.pb(cnt.back()+(i == -1));
cnt1.pb(cnt1.back()+(i == 1));
}
int ans = 1e9;
for(int i = 0;i<n-k;i++){
int le = i+1;
int ri = i+k;
// cout<<cho(cnt[ri]-cnt[le],l[i]-(cnt1[ri]-cnt1[le-1]))<<" "<<le<<" "<<ri<<endl;
if(cho(cnt[ri]-cnt[le],l[i]-(cnt1[ri]-cnt1[le-1])) >=1)
ans = min(ans,cho(cnt[ri]-cnt[le],l[i]-(cnt1[ri]-cnt1[le-1])));
}
cout<<ans%M<<endl;
// int k1 = 0;
// int c1= 0;
// for(int i =0;i<k;i++){
// if(a[i] == -1){
// k1++;
// }
// else{
// if(a[i] == 1){
// c1++;
// }
// }
// }
// int k2 = 0;
// int c2= 0;
// for(int i = n-1;i>=n-k;i--){
// if(a[i] == -1){
// k2++;
// }
// if(a[i] == 1){
// c2++;
// }
// }
// if(ans1<min(cho(k2,l.back()-c2),cho(k1,l[0]-c1))){
// int tt = 0;
// while(1){
// tt++;
// }
// }
// cout<<min(cho(k2,l.back()-c2),cho(k1,l[0]-c1))<<endl;
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
fact.resize(1e6+12,0);
fact[0] = 1;
for(int i = 1;i<=1e6+10;i++){
fact[i] = fact[i-1]*i;
fact[i]%=M;
}
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |