Submission #234538

# Submission time Handle Problem Language Result Execution time Memory
234538 2020-05-24T12:36:30 Z shayan_p Ljetopica (COI19_ljetopica) C++14
100 / 100
37 ms 8440 KB
// Never let them see you bleed...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn = 1010 , mod = 1e9 + 7, inf = 1e9 + 10;

int fac[maxn], ifac[maxn];

int Pow(int a, int b){
    int ans = 1;
    for(; b; b>>=1, a = 1ll * a * a % mod)
	if(b & 1)
	    ans = 1ll * ans * a % mod;
    return ans;
}
int C(int n, int k){
    if(n < 0 || k < 0 || n < k)
	return 0;
    return 1ll * fac[n] * ifac[k] %mod * ifac[n-k] % mod;
}

int S[maxn], L[maxn], R[maxn], n, k, ans;
int dp[maxn][maxn][2], tw[maxn], NW;

void go(int pos, bool ch, int used, bool needL, bool needR){
    if(used > k)
	return;
    if(pos == n-1){
	if(used == k)
	    ans = (ans + NW) % mod;
	return;
    }
    if(!needL && !needR){
	ans = (1ll * ans + 1ll * C(n-1-pos, k-used) * NW) % mod;
	ans = (1ll * ans + dp[pos][k-used][ch]) % mod;
	return;
    }
    for(int x : {0, 1}){
	if(needL && x < L[pos])
	    continue;
	if(needR && R[pos] < x)
	    continue;
	bool ch2 = S[pos] != x;
	NW = (NW + tw[n-2-pos] * x) % mod;
	go(pos+1, ch2, used + (ch != ch2), needL && x == L[pos], needR && x == R[pos]);
	NW = (NW - tw[n-2-pos] * x) % mod;
    }
}

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

    fac[0] = 1;
    for(int i = 1; i < maxn; i++)
	fac[i] = 1ll * i * fac[i-1] % mod;
    ifac[maxn-1] = Pow(fac[maxn-1], mod-2);
    for(int i = maxn-2; i >= 0; i--)
	ifac[i] = 1ll * ifac[i+1] * (i+1) % mod;
    tw[0] = 1;
    for(int i = 1; i < maxn; i++)
	tw[i] = 2ll * tw[i-1] % mod;
    
    cin >> n >> k;
    string s, l, r;
    cin >> s >> l >> r;
    for(int i = 0; i < n-1; i++)
	S[i] = s[i] == 'R', L[i] = l[i+1] == '1', R[i] = r[i+1] == '1';
    for(int i = n-2; i >= 0; i--){
	for(int j = 0; j <= k; j++){
	    for(int w : {0, 1}){
		dp[i][j][w] = 1ll * (1ll * C(n-2-i, j) * (S[i] ^ w) + 1ll * C(n-2-i, j-1) * (S[i] ^ w ^ 1)) * tw[n-2-i] % mod;
		dp[i][j][w] = ( 1ll * dp[i][j][w] + 1ll * dp[i+1][j][w] + 1ll * (j == 0 ? 0 : dp[i+1][j-1][w ^ 1]) ) % mod;
	    }
	}
    }
    NW = tw[n-1], go(0, 0, 0, 1, 1);
    NW = tw[n-1], go(0, 1, 0, 1, 1);
    if(ans < 0)
	ans+= mod;
    return cout << ans << endl, 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4480 KB Output is correct
2 Correct 6 ms 4352 KB Output is correct
3 Correct 6 ms 4096 KB Output is correct
4 Correct 6 ms 3712 KB Output is correct
5 Correct 6 ms 3712 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 6 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 8320 KB Output is correct
2 Correct 22 ms 7552 KB Output is correct
3 Correct 25 ms 7936 KB Output is correct
4 Correct 37 ms 8440 KB Output is correct
5 Correct 22 ms 7424 KB Output is correct
6 Correct 37 ms 8312 KB Output is correct
7 Correct 16 ms 6272 KB Output is correct
8 Correct 23 ms 7928 KB Output is correct
9 Correct 7 ms 4608 KB Output is correct
10 Correct 22 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4480 KB Output is correct
2 Correct 6 ms 4352 KB Output is correct
3 Correct 6 ms 4096 KB Output is correct
4 Correct 6 ms 3712 KB Output is correct
5 Correct 6 ms 3712 KB Output is correct
6 Correct 6 ms 3456 KB Output is correct
7 Correct 6 ms 3200 KB Output is correct
8 Correct 6 ms 3072 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 27 ms 8320 KB Output is correct
25 Correct 22 ms 7552 KB Output is correct
26 Correct 25 ms 7936 KB Output is correct
27 Correct 37 ms 8440 KB Output is correct
28 Correct 22 ms 7424 KB Output is correct
29 Correct 37 ms 8312 KB Output is correct
30 Correct 16 ms 6272 KB Output is correct
31 Correct 23 ms 7928 KB Output is correct
32 Correct 7 ms 4608 KB Output is correct
33 Correct 22 ms 7552 KB Output is correct
34 Correct 36 ms 8184 KB Output is correct
35 Correct 17 ms 6016 KB Output is correct
36 Correct 22 ms 7424 KB Output is correct
37 Correct 32 ms 7936 KB Output is correct
38 Correct 12 ms 4992 KB Output is correct
39 Correct 30 ms 7680 KB Output is correct
40 Correct 10 ms 4864 KB Output is correct
41 Correct 26 ms 8064 KB Output is correct
42 Correct 30 ms 8312 KB Output is correct
43 Correct 30 ms 8056 KB Output is correct
44 Correct 30 ms 7680 KB Output is correct
45 Correct 15 ms 5888 KB Output is correct
46 Correct 30 ms 7680 KB Output is correct
47 Correct 30 ms 7808 KB Output is correct
48 Correct 21 ms 7040 KB Output is correct
49 Correct 7 ms 4352 KB Output is correct
50 Correct 32 ms 8184 KB Output is correct
51 Correct 20 ms 6912 KB Output is correct
52 Correct 21 ms 7168 KB Output is correct
53 Correct 34 ms 8316 KB Output is correct
54 Correct 17 ms 6272 KB Output is correct
55 Correct 30 ms 8312 KB Output is correct
56 Correct 33 ms 8312 KB Output is correct
57 Correct 10 ms 5120 KB Output is correct
58 Correct 29 ms 8320 KB Output is correct