# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1113226 | ntdaccode | Binaria (CCO23_day1problem1) | C++17 | 90 ms | 41488 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fori(i,a,b) for(int i = a;i <= b; i++)
#define int long long
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M = 2e6 + 10;
const int N = 1e3 + 10;
const int mod = 1e6 + 3;
int n,k,a[M],id[M];
int finded(int u)
{
return (id[u] < 0 ? u : id[u] = finded(id[u]));
}
void unioned(int u,int v)
{
u = finded(u);
v = finded(v);
if(u == v) return ;
if(u < v) swap(u,v);
id[u] += id[v];
id[v] = u;
return ;
}
int power(int a,int m)
{
if(m == 0) return 1;
int t = power(a,m/2);
t = (t * t) % mod;
if(m % 2) t = (t * a) % mod;
return t;
}
int C(int k,int n)
{
if(k > n) return 0;
int res = 1;
for(int i = k + 1;i <= n; i++) res = (res * (i % mod) ) % mod;
int du = 1;
for(int i = 1;i <= n - k; i++) du = (du * (i % mod) ) % mod;
du = power(du,mod - 2) % mod;
return (res * du) % mod;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r"))
{
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
for(int i = 1;i <= n - k + 1; i++) cin >> a[i];
for(int i = 1;i <= 2 * n + 1; i++) id[i] = -1;
for(int i = 1;i <= n - k ; i++) {
if(abs(a[i] - a[i + 1]) > 1 || a[i] > k || a[i + 1] > k ) {
cout << 0 ;
return 0;
}
if(a[i] == a[i + 1]) {
unioned(i,i + k );
unioned(i + n,i + k + n);
//cout << i + k - 1 << " " << i + k - 1 + n << "\n";
}
if(a[i] == a[i + 1] + 1) {
unioned(i + n,i + k);
unioned(i,2 * n + 1);
unioned(i + k + n,2 * n + 1);
}
if(a[i] == a[i + 1] - 1) {
unioned(i,i + k + n);
unioned(i + n, 2 * n + 1);
unioned(i + k, 2 * n + 1);
}
}
int K = k;
for(int i = 1;i <= k ; i++) {
int u = finded(i);
int v = finded(i + n);
if(u == 2 * n + 1 && v == 2 * n + 1) {
cout << 0;
return 0;
}
if(v == 2 * n + 1) K--;
if(u == 2 * n + 1) K--,a[1]--;
}
cout << C(a[1],K) % mod;
}
Compilation message (stderr)
# | 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... |