Submission #117833

# Submission time Handle Problem Language Result Execution time Memory
117833 2019-06-17T08:42:09 Z 임유진(#2881) Ljetopica (COI19_ljetopica) C++14
100 / 100
33 ms 31736 KB
#include<stdio.h>
#include<stdlib.h>

#define MAXN 1005

const int MOD=1e9 + 7;

char ans[MAXN];
char A[MAXN], B[MAXN];
int two[MAXN];
long long dp[MAXN][MAXN][2][2];
bool cA[MAXN], cB[MAXN];

void add(long long* a, long long b) {
	*a = (*a + b) % MOD;
}

int main() {
	int N, K;
	long long res;

	//freopen("input.txt", "r", stdin);
	
	scanf("%d%d", &N, &K);
	scanf("%s\n%s\n%s", ans, A, B);

	if (A[N] != 0 || B[N - 1] == 0) {
		printf("0");
		return 0;
	}
	if (A[N - 1] == 0) {
		A[0] = '1';
		for (int i = 1; i < N; i++) A[i] = '0';
	}
	if (B[N] != 0) for (int i = 0; i < N; i++) B[i] = '1';
	//printf("%s*%s*%s", A, B, ans);

	for (int i = 0; i < N - 1; i++) {
		cA[i] = ((ans[i] == 'L' && A[i+1] == '0') || (ans[i] == 'R' && A[i+1] == '1'));
		cB[i] = ((ans[i] == 'L' && B[i+1] == '0') || (ans[i] == 'R' && B[i+1] == '1'));
		//printf("[%d %d]\n", cA[i]?1:0, cB[i]?1:0);
	}

	two[0] = 1;
	for (int i = 1; i < N; i++) two[i] = two[i - 1] * 2 % MOD;

	dp[N - 1][0][0][0] = dp[N-1][0][1][0]=1ll;
	//printf("%lld\n", dp[2][0][1][0]);
	//for (int i = 0; i < N; i++)
		//for (int j = 0; j <= K; j++) printf("i=%d, j=%d: (%lld %lld), (%lld %lld)\n", i, j, dp[i][j][0][0], dp[i][j][0][1], dp[i][j][1][0], dp[i][j][1][1]);
	for (int i = N - 2; i >= 0; i--) for(int j=0; j<=K; j++){
		dp[i][j][0][0] = dp[i + 1][j][0][0];
		dp[i][j][1][0] = dp[i + 1][j][1][0];
		if (j > 0) {
			add(&dp[i][j][0][0], dp[i + 1][j - 1][1][0]);
			add(&dp[i][j][1][0], dp[i + 1][j - 1][0][0]);
		}
		dp[i][j][0][1] = dp[i + 1][j][0][1];
		dp[i][j][1][1] = dp[i + 1][j][1][1];
		if (ans[i] == 'L') add(&dp[i][j][1][1], two[N - i - 2] * dp[i + 1][j][1][0]);
		else add(&dp[i][j][0][1], two[N - i - 2] * dp[i + 1][j][0][0]);
		if (j > 0) {
			add(&dp[i][j][0][1], dp[i + 1][j - 1][1][1]);
			add(&dp[i][j][1][1], dp[i + 1][j - 1][0][1]);
			if (ans[i] == 'L') add(&dp[i][j][0][1], two[N - i - 2] * dp[i + 1][j - 1][1][0]);
			else add(&dp[i][j][1][1], two[N - i - 2] * dp[i + 1][j - 1][0][0]);
		}
	}

	//for (int i = 0; i < N; i++)
		//for (int j = 0; j <= K; j++) printf("i=%d, j=%d: (%lld %lld), (%lld %lld)\n", i, j, dp[i][j][0][0], dp[i][j][0][1], dp[i][j][1][0], dp[i][j][1][1]);

	res = (dp[0][K][0][1] + dp[0][K][1][1] + two[N - 1] * (dp[0][K][0][0] + dp[0][K][1][0])) % MOD;
	//printf("%lld\n", res);
	long long sA=two[N-1], sB=two[N-1];
	int KA = K, KB=K;
	for (int i = 0; i < N - 1; i++) {
		if (A[i + 1] == '1') {
			//printf("@");
			if (i>0&&cA[i - 1] == cA[i]) KA--;
			//printf("(%d %lld)", KA, sA);
			if (KA >= 0) res = (res - dp[i + 1][KA][cA[i] ? 1 : 0][1] - sA * dp[i + 1][KA][cA[i] ? 1 : 0][0]) % MOD;
			if (KA > 0) res = (res - dp[i + 1][KA-1][cA[i] ? 1 : 0][1] - sA * dp[i + 1][KA-1][cA[i] ? 1 : 0][0]) % MOD;
			if (i>0&&cA[i - 1] == cA[i]) KA++;
			//printf("%lld", res);
			add(&sA, two[N - i - 2]);
		}
		if (B[i + 1] == '0') {
			//printf("$");
			if (i>0&&cB[i - 1] == cB[i]) KB--;
			//printf("(%d %lld)\n", KB, sB);
			if (KB >= 0) res = (res - dp[i + 1][KB][cB[i] ? 1 : 0][1] - (sB+two[N-i-2]) * dp[i + 1][KB][cB[i] ? 1 : 0][0]) % MOD;
			if(KB>0) res = (res - dp[i + 1][KB-1][cB[i] ? 1 : 0][1] - (sB+two[N-i-2]) * dp[i + 1][KB-1][cB[i] ? 1 : 0][0]) % MOD;
			if (i>0&&cB[i - 1] == cB[i]) KB++;
			//printf("%lld", res);
		}
		else add(&sB, two[N - i - 2]);
		if (i > 0) {
			if (cA[i - 1] != cA[i]) KA--;
			if (cB[i - 1] != cB[i]) KB--;
		}
		res = (res + MOD) % MOD;
	}
	
	printf("%lld", res);
	return 0;
}

Compilation message

ljetopica.cpp: In function 'int main()':
ljetopica.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
ljetopica.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s\n%s\n%s", ans, A, B);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4352 KB Output is correct
2 Correct 4 ms 4224 KB Output is correct
3 Correct 4 ms 3968 KB Output is correct
4 Correct 4 ms 3840 KB Output is correct
5 Correct 4 ms 3584 KB Output is correct
6 Correct 4 ms 3328 KB Output is correct
7 Correct 4 ms 3200 KB Output is correct
8 Correct 4 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 22272 KB Output is correct
2 Correct 18 ms 16768 KB Output is correct
3 Correct 18 ms 18560 KB Output is correct
4 Correct 31 ms 31736 KB Output is correct
5 Correct 16 ms 16000 KB Output is correct
6 Correct 33 ms 31736 KB Output is correct
7 Correct 14 ms 11520 KB Output is correct
8 Correct 22 ms 18296 KB Output is correct
9 Correct 5 ms 4992 KB Output is correct
10 Correct 17 ms 16996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4352 KB Output is correct
2 Correct 4 ms 4224 KB Output is correct
3 Correct 4 ms 3968 KB Output is correct
4 Correct 4 ms 3840 KB Output is correct
5 Correct 4 ms 3584 KB Output is correct
6 Correct 4 ms 3328 KB Output is correct
7 Correct 4 ms 3200 KB Output is correct
8 Correct 4 ms 2944 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 24 ms 22272 KB Output is correct
25 Correct 18 ms 16768 KB Output is correct
26 Correct 18 ms 18560 KB Output is correct
27 Correct 31 ms 31736 KB Output is correct
28 Correct 16 ms 16000 KB Output is correct
29 Correct 33 ms 31736 KB Output is correct
30 Correct 14 ms 11520 KB Output is correct
31 Correct 22 ms 18296 KB Output is correct
32 Correct 5 ms 4992 KB Output is correct
33 Correct 17 ms 16996 KB Output is correct
34 Correct 27 ms 26360 KB Output is correct
35 Correct 12 ms 11648 KB Output is correct
36 Correct 16 ms 16640 KB Output is correct
37 Correct 30 ms 30200 KB Output is correct
38 Correct 9 ms 7552 KB Output is correct
39 Correct 29 ms 28672 KB Output is correct
40 Correct 7 ms 7040 KB Output is correct
41 Correct 20 ms 20352 KB Output is correct
42 Correct 24 ms 25080 KB Output is correct
43 Correct 26 ms 24832 KB Output is correct
44 Correct 27 ms 28544 KB Output is correct
45 Correct 11 ms 11008 KB Output is correct
46 Correct 23 ms 23040 KB Output is correct
47 Correct 25 ms 24180 KB Output is correct
48 Correct 16 ms 15616 KB Output is correct
49 Correct 5 ms 4736 KB Output is correct
50 Correct 29 ms 27512 KB Output is correct
51 Correct 15 ms 14592 KB Output is correct
52 Correct 15 ms 15352 KB Output is correct
53 Correct 31 ms 31736 KB Output is correct
54 Correct 13 ms 11904 KB Output is correct
55 Correct 29 ms 27008 KB Output is correct
56 Correct 32 ms 30200 KB Output is correct
57 Correct 8 ms 6912 KB Output is correct
58 Correct 26 ms 24312 KB Output is correct