Submission #117669

# Submission time Handle Problem Language Result Execution time Memory
117669 2019-06-17T05:32:57 Z 김세빈(#2878) Ljetopica (COI19_ljetopica) C++14
100 / 100
30 ms 16256 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;

char S[1010], A[1010], B[1010];
ll dp[1010][1010], C[1010][1010], T[1010];
ll n, k, ans;

ll f()
{
	ll i, j, x;

	C[n + 1][0] = 1;

	for(i=n; i>=1; i--){
		for(j=0; j<=k; j++){
			dp[i][j] = dp[i + 1][j];
			C[i][j] = C[i + 1][j];
			if((k - j & 1) ^ (S[i - 1] == 'R')) dp[i][j] = (dp[i][j] + T[n - i] * C[i + 1][j]) % mod;

			if(j){
				dp[i][j] = (dp[i][j] + dp[i + 1][j - 1]) % mod;
				C[i][j] = (C[i][j] + C[i + 1][j - 1]) % mod;
				if((k - j + 1 & 1) ^ (S[i - 1] == 'R')) dp[i][j] = (dp[i][j] + T[n - i] * C[i + 1][j - 1]) % mod;
			}
		}
	}
/*
	for(i=1; i<=n; i++){
		for(j=0; j<=k; j++){
			printf("%lld ", dp[i][j]);
		}
		printf("\n");
	}
*/
	ll s1, s2, c1, c2;

	s1 = s2 = 0; c1 = c2 = 0;

	for(i=1, j=k, x=0; i<=n; i++){
		if(A[i] == '0'){
			if((k - j & 1) ^ (S[i - 1] == 'R')) j --;
			x = x * 2 % mod;
		}
		else{
			if((k - j & 1) ^ (S[i - 1] == 'L')){
				s1 = (s1 + dp[i + 1][j] + C[i + 1][j] * (x * 2) % mod * T[n - i]) % mod;
				c1 = (c1 + C[i + 1][j]) % mod;
				j --;
			}
			else{
				if(j){
					s1 = (s1 + dp[i + 1][j - 1] + C[i + 1][j - 1] * (x * 2) % mod * T[n - i]) % mod;
					c1 = (c1 + C[i + 1][j - 1]) % mod;
				}
			}
			x = (x * 2 + 1) % mod;
		}

		if(j < 0) break;
	}

	for(i=1, j=k, x=0; i<=n; i++){
		if(B[i] == '1'){
			if((k - j & 1) ^ (S[i - 1] == 'L')) j --;
			x = (x * 2 + 1) % mod;
		}
		else{
			if((k - j & 1) ^ (S[i - 1] == 'R')){
				s2 = (s2 + dp[i + 1][j] + C[i + 1][j] * (x * 2 + 1) % mod * T[n - i]) % mod;
				c2 = (c2 + C[i + 1][j]) % mod; 
				j --;
			}
			else{
				if(j){
					s2 = (s2 + dp[i + 1][j - 1] + C[i + 1][j - 1] * (x * 2 + 1) % mod * T[n - i]) % mod;
					c2 = (c2 + C[i + 1][j - 1]) % mod;
				}
			}
			x = x * 2 % mod;
		}

		if(j < 0) break;
	}

	return ((dp[1][k] - s1 - s2) + (C[1][k] - c1 - c2) * T[n]) % mod;
}

int main()
{
	ll i;

	scanf("%lld%lld", &n, &k); n --;
	scanf("%s%s%s", S, A, B);

	T[0] = 1;

	for(i=1; i<=n; i++){
		T[i] = T[i - 1] * 2 % mod;
	}

	ans = f();	

	for(i=0; i<n; i++){
		S[i] = (int)'L' + 'R' - S[i];
	}

	ans = (ans + f()) % mod;

	printf("%lld\n", (ans + mod) % mod);

	return 0;
}

Compilation message

ljetopica.cpp: In function 'll f()':
ljetopica.cpp:23:10: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
    if((k - j & 1) ^ (S[i - 1] == 'R')) dp[i][j] = (dp[i][j] + T[n - i] * C[i + 1][j]) % mod;
        ~~^~~
ljetopica.cpp:28:15: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
     if((k - j + 1 & 1) ^ (S[i - 1] == 'R')) dp[i][j] = (dp[i][j] + T[n - i] * C[i + 1][j - 1]) % mod;
         ~~~~~~^~~
ljetopica.cpp:46:10: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
    if((k - j & 1) ^ (S[i - 1] == 'R')) j --;
        ~~^~~
ljetopica.cpp:50:10: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
    if((k - j & 1) ^ (S[i - 1] == 'L')){
        ~~^~~
ljetopica.cpp:69:10: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
    if((k - j & 1) ^ (S[i - 1] == 'L')) j --;
        ~~^~~
ljetopica.cpp:73:10: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
    if((k - j & 1) ^ (S[i - 1] == 'R')){
        ~~^~~
ljetopica.cpp: In function 'int main()':
ljetopica.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld", &n, &k); n --;
  ~~~~~^~~~~~~~~~~~~~~~~~~~
ljetopica.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s%s%s", S, A, B);
  ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8320 KB Output is correct
2 Correct 7 ms 7936 KB Output is correct
3 Correct 7 ms 7552 KB Output is correct
4 Correct 7 ms 7168 KB Output is correct
5 Correct 6 ms 6784 KB Output is correct
6 Correct 6 ms 6400 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 5 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 16128 KB Output is correct
2 Correct 17 ms 14464 KB Output is correct
3 Correct 18 ms 15460 KB Output is correct
4 Correct 30 ms 16128 KB Output is correct
5 Correct 17 ms 14208 KB Output is correct
6 Correct 26 ms 16168 KB Output is correct
7 Correct 13 ms 11904 KB Output is correct
8 Correct 19 ms 15360 KB Output is correct
9 Correct 8 ms 8576 KB Output is correct
10 Correct 18 ms 14564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8320 KB Output is correct
2 Correct 7 ms 7936 KB Output is correct
3 Correct 7 ms 7552 KB Output is correct
4 Correct 7 ms 7168 KB Output is correct
5 Correct 6 ms 6784 KB Output is correct
6 Correct 6 ms 6400 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 5 ms 5504 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 0 ms 512 KB Output is correct
24 Correct 20 ms 16128 KB Output is correct
25 Correct 17 ms 14464 KB Output is correct
26 Correct 18 ms 15460 KB Output is correct
27 Correct 30 ms 16128 KB Output is correct
28 Correct 17 ms 14208 KB Output is correct
29 Correct 26 ms 16168 KB Output is correct
30 Correct 13 ms 11904 KB Output is correct
31 Correct 19 ms 15360 KB Output is correct
32 Correct 8 ms 8576 KB Output is correct
33 Correct 18 ms 14564 KB Output is correct
34 Correct 23 ms 15964 KB Output is correct
35 Correct 13 ms 11392 KB Output is correct
36 Correct 17 ms 14080 KB Output is correct
37 Correct 24 ms 15360 KB Output is correct
38 Correct 11 ms 9472 KB Output is correct
39 Correct 22 ms 14720 KB Output is correct
40 Correct 9 ms 9216 KB Output is correct
41 Correct 19 ms 15744 KB Output is correct
42 Correct 23 ms 16000 KB Output is correct
43 Correct 22 ms 15360 KB Output is correct
44 Correct 24 ms 14720 KB Output is correct
45 Correct 14 ms 11264 KB Output is correct
46 Correct 21 ms 14848 KB Output is correct
47 Correct 22 ms 15104 KB Output is correct
48 Correct 17 ms 13440 KB Output is correct
49 Correct 7 ms 8064 KB Output is correct
50 Correct 22 ms 15744 KB Output is correct
51 Correct 15 ms 13184 KB Output is correct
52 Correct 15 ms 13696 KB Output is correct
53 Correct 25 ms 16128 KB Output is correct
54 Correct 13 ms 12160 KB Output is correct
55 Correct 22 ms 16128 KB Output is correct
56 Correct 24 ms 16116 KB Output is correct
57 Correct 10 ms 9600 KB Output is correct
58 Correct 22 ms 16256 KB Output is correct