Submission #1027242

# Submission time Handle Problem Language Result Execution time Memory
1027242 2024-07-19T02:38:20 Z nathan4690 Binaria (CCO23_day1problem1) C++17
25 / 25
71 ms 29776 KB
// CCO '23 P1 - Binaria
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18, mod=1e6+3;

ll n, k, a[maxn], l, r, ans;
char res[maxn];

ll fac[maxn], inv[maxn];

ll powmod(ll base, ll exp){
    if(exp == 0) return 1ll;
    if(exp == 1) return base % mod;
    ll v = powmod(base, exp/2);
    if(exp % 2) return v*v%mod*base%mod;
    else return v*v%mod;
}

void preprocess(){
    fac[0] = 1;
    f1(i,n) fac[i] = fac[i-1] * i % mod;
    inv[n] = powmod(fac[n], mod-2);
    inv[0] = 1;
    for(ll i=n-1;i>=1;i--){
        inv[i] = (inv[i+1] * (i+1)) % mod;
    }
}

ll C(ll n, ll k){
    if(k > n || k < 0) return 0ll;
    return fac[n] * inv[k] % mod * inv[n-k] % mod;
}

void solve(){
    // 0: character is 0
    // 1: character is 1
    // x: character is based on the (i-k)th character
    // ?: unknown
    f1(i,n) res[i] = '?';
    f1(i,n-k){
        l = i; r = i + k;
        if(abs(a[i+1] - a[i]) > 1){
            cout << 0;el;
            return;
        }
        if(a[i+1] > a[i]){
            res[r] = '1';
            ll idx = l;
            bool flag = false;
            while(res[idx] == 'x'){
                res[idx] = char('0' + (ll)flag);
                flag = !flag;
                idx = idx - k;
                // if(idx <= 0) break;
            }
            if((res[idx] == '1' && (!flag)) || (res[idx] == '0' && flag)){
                cout << 0;el;
                return;
            }
            res[idx] = char('0' + (ll)flag);
        }else if(a[i+1] == a[i]){
            if(res[l] == '?') res[r] = 'x';
            else res[r] = res[l];
        }else{
            res[r] = '0';
            ll idx = l;
            bool flag = true;
            while(res[idx] == 'x'){
                res[idx] = char('0' + (ll)flag);
                // flag = !flag;
                idx = idx - k;
                // if(idx <= 0) break;
            }
            if((res[idx] == '1' && (!flag)) || (res[idx] == '0' && flag)){
                cout << 0;el;
                return;
            }
            res[idx] = char('0' + (ll)flag);
        }
    }
    // f1(i,n) cout << res[i] << ' ';el;
    ll ccnt = 0;
    for(ll i=n-k+1;i<=n;i++){
        ccnt += (!(res[i] == '0' || res[i] == '1'));
        if(res[i] == '1') a[n-k+1]--;
    }
    // cout << ccnt << ' ' << a[n-k+1];el;
    cout << C(ccnt, a[n-k+1]);
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n >> k;
    preprocess();
    f1(i,n - k + 1) cin >> a[i];
    solve();
    return 0;
}

/*
Code by: Nguyen Viet Trung Nhan
Cau Giay Secondary School
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6612 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6612 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6628 KB Output is correct
31 Correct 1 ms 6744 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6612 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6628 KB Output is correct
31 Correct 1 ms 6744 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 50 ms 26704 KB Output is correct
36 Correct 55 ms 27388 KB Output is correct
37 Correct 48 ms 27800 KB Output is correct
38 Correct 49 ms 27664 KB Output is correct
39 Correct 48 ms 26708 KB Output is correct
40 Correct 51 ms 27736 KB Output is correct
41 Correct 69 ms 27784 KB Output is correct
42 Correct 49 ms 26764 KB Output is correct
43 Correct 47 ms 26612 KB Output is correct
44 Correct 48 ms 26648 KB Output is correct
45 Correct 45 ms 26752 KB Output is correct
46 Correct 47 ms 26708 KB Output is correct
47 Correct 44 ms 26708 KB Output is correct
48 Correct 46 ms 27984 KB Output is correct
49 Correct 46 ms 27728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6612 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6628 KB Output is correct
31 Correct 1 ms 6744 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 50 ms 26704 KB Output is correct
36 Correct 55 ms 27388 KB Output is correct
37 Correct 48 ms 27800 KB Output is correct
38 Correct 49 ms 27664 KB Output is correct
39 Correct 48 ms 26708 KB Output is correct
40 Correct 51 ms 27736 KB Output is correct
41 Correct 69 ms 27784 KB Output is correct
42 Correct 49 ms 26764 KB Output is correct
43 Correct 47 ms 26612 KB Output is correct
44 Correct 48 ms 26648 KB Output is correct
45 Correct 45 ms 26752 KB Output is correct
46 Correct 47 ms 26708 KB Output is correct
47 Correct 44 ms 26708 KB Output is correct
48 Correct 46 ms 27984 KB Output is correct
49 Correct 46 ms 27728 KB Output is correct
50 Correct 60 ms 29520 KB Output is correct
51 Correct 65 ms 29656 KB Output is correct
52 Correct 60 ms 29524 KB Output is correct
53 Correct 60 ms 29524 KB Output is correct
54 Correct 57 ms 29520 KB Output is correct
55 Correct 71 ms 29580 KB Output is correct
56 Correct 58 ms 29524 KB Output is correct
57 Correct 52 ms 28752 KB Output is correct
58 Correct 55 ms 29776 KB Output is correct
59 Correct 56 ms 29712 KB Output is correct
60 Correct 53 ms 28756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6612 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6628 KB Output is correct
31 Correct 1 ms 6744 KB Output is correct
32 Correct 2 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 50 ms 26704 KB Output is correct
36 Correct 55 ms 27388 KB Output is correct
37 Correct 48 ms 27800 KB Output is correct
38 Correct 49 ms 27664 KB Output is correct
39 Correct 48 ms 26708 KB Output is correct
40 Correct 51 ms 27736 KB Output is correct
41 Correct 69 ms 27784 KB Output is correct
42 Correct 49 ms 26764 KB Output is correct
43 Correct 47 ms 26612 KB Output is correct
44 Correct 48 ms 26648 KB Output is correct
45 Correct 45 ms 26752 KB Output is correct
46 Correct 47 ms 26708 KB Output is correct
47 Correct 44 ms 26708 KB Output is correct
48 Correct 46 ms 27984 KB Output is correct
49 Correct 46 ms 27728 KB Output is correct
50 Correct 60 ms 29520 KB Output is correct
51 Correct 65 ms 29656 KB Output is correct
52 Correct 60 ms 29524 KB Output is correct
53 Correct 60 ms 29524 KB Output is correct
54 Correct 57 ms 29520 KB Output is correct
55 Correct 71 ms 29580 KB Output is correct
56 Correct 58 ms 29524 KB Output is correct
57 Correct 52 ms 28752 KB Output is correct
58 Correct 55 ms 29776 KB Output is correct
59 Correct 56 ms 29712 KB Output is correct
60 Correct 53 ms 28756 KB Output is correct
61 Correct 11 ms 19036 KB Output is correct
62 Correct 12 ms 19032 KB Output is correct
63 Correct 12 ms 18916 KB Output is correct
64 Correct 13 ms 19036 KB Output is correct
65 Correct 44 ms 24404 KB Output is correct
66 Correct 30 ms 23380 KB Output is correct
67 Correct 66 ms 28756 KB Output is correct
68 Correct 16 ms 19288 KB Output is correct
69 Correct 12 ms 19032 KB Output is correct
70 Correct 12 ms 18904 KB Output is correct
71 Correct 12 ms 19036 KB Output is correct