답안 #134545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134545 2019-07-23T03:08:48 Z luciocf Ljetopica (COI19_ljetopica) C++14
100 / 100
135 ms 39808 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

const int maxn = 1e3+10;
const int mod = 1e9+7;

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

int n, k;
int pot[maxn];

char pos[maxn], A[maxn];

bool mark[maxn][maxn][2][2];

pii dp[maxn][maxn][2][2];

void change(void)
{
	int last = 0;
	for (int i = n-1; i >= 0; i--)
	{
		if (A[i] == '1')
		{
			last = i;
			break;
		}
	}

	A[last] = '0';
	for (int i = last+1; i < n; i++)
		A[i] = '1';
}

pii solve(int depth, int r, int q, int flag)
{
	if (mark[depth][r][q][flag]) return dp[depth][r][q][flag];
	if (depth == n && r != 0) return {0, 0};
	if (depth == n) return {0, 1};

	mark[depth][r][q][flag] = true;

	if (!r) 
	{
		char c = pos[depth];

		if (!q && c == 'L') c = 'R';
		else if (!q && c == 'R') c = 'L';

		if (c == 'L')
			return dp[depth][r][q][flag] = solve(depth+1, 0, q, (flag || (A[depth]=='1')));

		if (A[depth] == '1' || flag)
		{
			pii ans = solve(depth+1, 0, q, flag);

			return dp[depth][r][q][flag] = {(ans.ff + (1ll*pot[n-depth-1]*ans.ss)%mod)%mod, ans.ss};
		}

		return dp[depth][r][q][flag] = {0, 0};
	}

	char c = pos[depth];
	if (!q && c == 'L') c = 'R';
	else if (!q && c == 'R') c = 'L';

	if (c == 'L')
	{
		pii ans1 = solve(depth+1, r, q, (flag || (A[depth]=='1')));
		pii ans2 = solve(depth+1, r-1, !q, (flag || (A[depth]=='1')));

		return dp[depth][r][q][flag] = {(ans1.ff+ans2.ff)%mod, (ans1.ss+ans2.ss)%mod};
	}
	else
	{
		pii ans = {0, 0};

		if (A[depth] == '1' || flag)
		{
			pii sub1 = solve(depth+1, r, q, flag);
			pii sub2 = solve(depth+1, r-1, !q, flag);

			ans = {(sub1.ff+sub2.ff)%mod, (sub1.ss+sub2.ss)%mod};

			ans.ff = (ans.ff + ((1ll*pot[n-depth-1]*ans.ss)%mod))%mod;
		}

		return dp[depth][r][q][flag] = ans;
	}
}

int main(void)
{
	scanf("%d %d", &n, &k);

	pot[0] = 1;
	for (int i = 1; i <= n; i++)
		pot[i] = (2*pot[i-1])%mod;

	for (int i = 1; i < n; i++)
		scanf(" %c", &pos[i]);

	for (int i = 0; i < n; i++)
		scanf(" %c", &A[i]);

	change();

	int ans1 = 0;

	if (A[0] != '0')
	{
		pii sub1 = solve(1, k, 1, 0);
		memset(mark, 0, sizeof mark);
		pii sub2 = solve(1, k, 0, 0);

		ans1 = ((sub1.ff + sub2.ff)%mod + (1ll*pot[n-1]*sub1.ss + 1ll*pot[n-1]*sub2.ss)%mod)%mod;
	}

	memset(mark, 0, sizeof mark);

	for (int i = 0; i < n; i++)
		scanf(" %c", &A[i]);

	pii sub1 = solve(1, k, 1, 0);
	memset(mark, 0, sizeof mark);
	pii sub2 = solve(1, k, 0, 0);

	int ans2 = ((sub1.ff + sub2.ff)%mod + (1ll*pot[n-1]*sub1.ss + 1ll*pot[n-1]*sub2.ss)%mod)%mod;

	printf("%d\n", (ans2+mod-ans1)%mod);
}

Compilation message

ljetopica.cpp: In function 'int main()':
ljetopica.cpp:99: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:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &pos[i]);
   ~~~~~^~~~~~~~~~~~~~~~
ljetopica.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &A[i]);
   ~~~~~^~~~~~~~~~~~~~
ljetopica.cpp:127:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &A[i]);
   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8696 KB Output is correct
2 Correct 9 ms 8312 KB Output is correct
3 Correct 10 ms 8144 KB Output is correct
4 Correct 10 ms 7928 KB Output is correct
5 Correct 12 ms 7672 KB Output is correct
6 Correct 10 ms 7420 KB Output is correct
7 Correct 10 ms 7288 KB Output is correct
8 Correct 9 ms 7032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4472 KB Output is correct
2 Correct 6 ms 4344 KB Output is correct
3 Correct 6 ms 4472 KB Output is correct
4 Correct 6 ms 4472 KB Output is correct
5 Correct 6 ms 4600 KB Output is correct
6 Correct 6 ms 4472 KB Output is correct
7 Correct 7 ms 4472 KB Output is correct
8 Correct 6 ms 4472 KB Output is correct
9 Correct 6 ms 4476 KB Output is correct
10 Correct 6 ms 4344 KB Output is correct
11 Correct 6 ms 4472 KB Output is correct
12 Correct 6 ms 4348 KB Output is correct
13 Correct 6 ms 4472 KB Output is correct
14 Correct 6 ms 4472 KB Output is correct
15 Correct 6 ms 4476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 34168 KB Output is correct
2 Correct 55 ms 28436 KB Output is correct
3 Correct 57 ms 30456 KB Output is correct
4 Correct 101 ms 39672 KB Output is correct
5 Correct 61 ms 27484 KB Output is correct
6 Correct 130 ms 39808 KB Output is correct
7 Correct 39 ms 21112 KB Output is correct
8 Correct 60 ms 30300 KB Output is correct
9 Correct 13 ms 9744 KB Output is correct
10 Correct 57 ms 28664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8696 KB Output is correct
2 Correct 9 ms 8312 KB Output is correct
3 Correct 10 ms 8144 KB Output is correct
4 Correct 10 ms 7928 KB Output is correct
5 Correct 12 ms 7672 KB Output is correct
6 Correct 10 ms 7420 KB Output is correct
7 Correct 10 ms 7288 KB Output is correct
8 Correct 9 ms 7032 KB Output is correct
9 Correct 6 ms 4472 KB Output is correct
10 Correct 6 ms 4344 KB Output is correct
11 Correct 6 ms 4472 KB Output is correct
12 Correct 6 ms 4472 KB Output is correct
13 Correct 6 ms 4600 KB Output is correct
14 Correct 6 ms 4472 KB Output is correct
15 Correct 7 ms 4472 KB Output is correct
16 Correct 6 ms 4472 KB Output is correct
17 Correct 6 ms 4476 KB Output is correct
18 Correct 6 ms 4344 KB Output is correct
19 Correct 6 ms 4472 KB Output is correct
20 Correct 6 ms 4348 KB Output is correct
21 Correct 6 ms 4472 KB Output is correct
22 Correct 6 ms 4472 KB Output is correct
23 Correct 6 ms 4476 KB Output is correct
24 Correct 68 ms 34168 KB Output is correct
25 Correct 55 ms 28436 KB Output is correct
26 Correct 57 ms 30456 KB Output is correct
27 Correct 101 ms 39672 KB Output is correct
28 Correct 61 ms 27484 KB Output is correct
29 Correct 130 ms 39808 KB Output is correct
30 Correct 39 ms 21112 KB Output is correct
31 Correct 60 ms 30300 KB Output is correct
32 Correct 13 ms 9744 KB Output is correct
33 Correct 57 ms 28664 KB Output is correct
34 Correct 135 ms 36680 KB Output is correct
35 Correct 60 ms 21112 KB Output is correct
36 Correct 73 ms 27512 KB Output is correct
37 Correct 122 ms 36596 KB Output is correct
38 Correct 38 ms 14712 KB Output is correct
39 Correct 88 ms 33912 KB Output is correct
40 Correct 23 ms 13816 KB Output is correct
41 Correct 84 ms 31828 KB Output is correct
42 Correct 87 ms 35960 KB Output is correct
43 Correct 114 ms 34652 KB Output is correct
44 Correct 89 ms 33784 KB Output is correct
45 Correct 69 ms 20216 KB Output is correct
46 Correct 82 ms 32376 KB Output is correct
47 Correct 66 ms 33400 KB Output is correct
48 Correct 79 ms 26232 KB Output is correct
49 Correct 14 ms 9592 KB Output is correct
50 Correct 96 ms 36600 KB Output is correct
51 Correct 74 ms 25208 KB Output is correct
52 Correct 63 ms 26508 KB Output is correct
53 Correct 83 ms 39416 KB Output is correct
54 Correct 49 ms 21752 KB Output is correct
55 Correct 76 ms 37368 KB Output is correct
56 Correct 129 ms 38892 KB Output is correct
57 Correct 25 ms 13432 KB Output is correct
58 Correct 92 ms 35704 KB Output is correct