Submission #144805

# Submission time Handle Problem Language Result Execution time Memory
144805 2019-08-17T19:59:57 Z emilem Ljetopica (COI19_ljetopica) C++14
100 / 100
251 ms 63352 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;
const long long mod = 1000000007;

long long n, k;
string path, a, b;
vector<long long> pow2(2000);
long long dp[1005][1005][2][2], cnt[1005][1005][2][2];

void Add(long long depth, long long steps, long long ok, long long atBound, long long depth2, long long steps2, long long ok2, long long atBound2, char dir)
{
	if (max(depth, depth2) > n || max(steps, steps2) > k)
		return;
	if (depth == 2 && steps == 0 && ok == 0 && atBound == 1)
		cerr << "";
	dp[depth][steps][ok][atBound] += 2 * dp[depth2][steps2][ok2][atBound2];
	if (dir == 'R')
		dp[depth][steps][ok][atBound] += cnt[depth2][steps2][ok2][atBound2];
	dp[depth][steps][ok][atBound] %= mod;
	cnt[depth][steps][ok][atBound] += cnt[depth2][steps2][ok2][atBound2];
	cnt[depth][steps][ok][atBound] %= mod;
}
long long Solve(string bound)
{
	for (long long depth = 1; depth <= n; ++depth)
		for (long long steps = 0; steps <= k; ++steps)
			for (long long i = 0; i <= 1; ++i)
				for (long long j = 0; j <= 1; ++j)
					cnt[depth][steps][i][j] = dp[depth][steps][i][j] = 0;
	dp[1][0][0][1] = dp[1][0][1][1] = 1;
	cnt[1][0][0][1] = cnt[1][0][1][1] = 1;
	for (long long depth = 1; depth < n; ++depth)
		for (long long steps = 0; steps <= k; ++steps)
			for (long long ok = 0; ok <= 1; ++ok)
			{
				// += dp[][][][0]
				if (depth == 2)
					cerr << "";
				if (path[depth - 1] == (ok ? 'L' : 'R'))
					Add(depth + 1, steps, ok, 0, depth, steps, ok, 0, 'L');
				else
					Add(depth + 1, steps, ok, 0, depth, steps, ok, 0, 'R');
				if (path[depth - 1] == (!ok ? 'L' : 'R'))
					Add(depth + 1, steps + 1, !ok, 0, depth, steps, ok, 0, 'L');
				else
					Add(depth + 1, steps + 1, !ok, 0, depth, steps, ok, 0, 'R');
				// += dp[][][][1]
				if (ok)
				{
					if (path[depth - 1] == 'L')
						Add(depth + 1, steps, ok, bound[depth - 1] == 'R' ? 0 : 1, depth, steps, ok, 1, 'L');
					else if (bound[depth - 1] == 'R')
						Add(depth + 1, steps, ok, 1, depth, steps, ok, 1, 'R');
				}
				else
				{
					if (path[depth - 1] == 'R')
						Add(depth + 1, steps, ok, bound[depth - 1] == 'R' ? 0 : 1, depth, steps, ok, 1, 'L');
					else if (bound[depth - 1] == 'R')
						Add(depth + 1, steps, ok, 1, depth, steps, ok, 1, 'R');
				}
				if (!ok)
				{
					if (path[depth - 1] == 'L')
						Add(depth + 1, steps + 1, !ok, bound[depth - 1] == 'R' ? 0 : 1, depth, steps, ok, 1, 'L');
					else if (bound[depth - 1] == 'R')
						Add(depth + 1, steps + 1, !ok, 1, depth, steps, ok, 1, 'R');
				}
				else
				{
					if (path[depth - 1] == 'R')
						Add(depth + 1, steps + 1, !ok, bound[depth - 1] == 'R' ? 0 : 1, depth, steps, ok, 1, 'L');
					else if (bound[depth - 1] == 'R')
						Add(depth + 1, steps + 1, !ok, 1, depth, steps, ok, 1, 'R');
				}
			}
	/*
	for (long long depth = 1; depth <= n; ++depth)
		for (long long steps = 0; steps <= k; ++steps)
			for (long long i = 0; i <= 1; ++i)
				for (long long j = 0; j <= 1; ++j)
				{
					cout << "dp[" << depth << "][" << steps << "][" << i << "][" << j << "] = " << dp[depth][steps][i][j] << '\t';
					cout << "cnt[" << depth << "][" << steps << "][" << i << "][" << j << "] = " << cnt[depth][steps][i][j] << endl;
				}
	*/
	long long ans = 0;
	for (long long i = 0; i <= 1; ++i)
		for (long long j = 0; j <= 1; ++j)
		{
			ans += dp[n][k][i][j];
			ans %= mod;
		}
	return ans;
}
int main()
{
	pow2[0] = 1;
	for (long long i = 1; i < pow2.size(); ++i)
		pow2[i] = (pow2[i - 1] + pow2[i - 1]) % mod;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> n >> k;
	cin >> path;
	cin >> a >> b;
	reverse(a.begin(), a.end());
	for (long long i = 0; i < a.length(); ++i)
		if (a[i] == '0')
			a[i] = '1';
		else
		{
			a[i] = '0';
			if (i + 1 == a.length())
				a.pop_back();
			break;
		}
	reverse(b.begin(), b.end());
	a.pop_back();
	b.pop_back();
	for (long long i = 0; i < a.length(); ++i)
		a[i] = (a[i] == '0') ? 'L' : 'R';
	for (long long i = 0; i < b.length(); ++i)
		b[i] = (b[i] == '0') ? 'L' : 'R';
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	cout << (Solve(b) - Solve(a) + mod) % mod << endl;

	char I;
	cin >> I;
}

Compilation message

ljetopica.cpp: In function 'int main()':
ljetopica.cpp:103:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (long long i = 1; i < pow2.size(); ++i)
                        ~~^~~~~~~~~~~~~
ljetopica.cpp:112:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (long long i = 0; i < a.length(); ++i)
                        ~~^~~~~~~~~~~~
ljetopica.cpp:118:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (i + 1 == a.length())
        ~~~~~~^~~~~~~~~~~~~
ljetopica.cpp:125:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (long long i = 0; i < a.length(); ++i)
                        ~~^~~~~~~~~~~~
ljetopica.cpp:127:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (long long i = 0; i < b.length(); ++i)
                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8440 KB Output is correct
2 Correct 8 ms 8056 KB Output is correct
3 Correct 8 ms 7672 KB Output is correct
4 Correct 7 ms 7288 KB Output is correct
5 Correct 7 ms 6904 KB Output is correct
6 Correct 7 ms 6520 KB Output is correct
7 Correct 6 ms 6008 KB Output is correct
8 Correct 6 ms 5624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
4 Correct 3 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 44344 KB Output is correct
2 Correct 102 ms 33272 KB Output is correct
3 Correct 126 ms 36856 KB Output is correct
4 Correct 251 ms 63352 KB Output is correct
5 Correct 97 ms 31736 KB Output is correct
6 Correct 240 ms 63344 KB Output is correct
7 Correct 63 ms 22652 KB Output is correct
8 Correct 114 ms 36220 KB Output is correct
9 Correct 14 ms 9720 KB Output is correct
10 Correct 117 ms 33656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 8440 KB Output is correct
2 Correct 8 ms 8056 KB Output is correct
3 Correct 8 ms 7672 KB Output is correct
4 Correct 7 ms 7288 KB Output is correct
5 Correct 7 ms 6904 KB Output is correct
6 Correct 7 ms 6520 KB Output is correct
7 Correct 6 ms 6008 KB Output is correct
8 Correct 6 ms 5624 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 3 ms 632 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 3 ms 632 KB Output is correct
15 Correct 2 ms 632 KB Output is correct
16 Correct 2 ms 632 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 632 KB Output is correct
22 Correct 2 ms 632 KB Output is correct
23 Correct 2 ms 632 KB Output is correct
24 Correct 154 ms 44344 KB Output is correct
25 Correct 102 ms 33272 KB Output is correct
26 Correct 126 ms 36856 KB Output is correct
27 Correct 251 ms 63352 KB Output is correct
28 Correct 97 ms 31736 KB Output is correct
29 Correct 240 ms 63344 KB Output is correct
30 Correct 63 ms 22652 KB Output is correct
31 Correct 114 ms 36220 KB Output is correct
32 Correct 14 ms 9720 KB Output is correct
33 Correct 117 ms 33656 KB Output is correct
34 Correct 186 ms 52588 KB Output is correct
35 Correct 66 ms 22920 KB Output is correct
36 Correct 104 ms 33016 KB Output is correct
37 Correct 220 ms 60280 KB Output is correct
38 Correct 38 ms 14812 KB Output is correct
39 Correct 193 ms 56952 KB Output is correct
40 Correct 30 ms 13816 KB Output is correct
41 Correct 128 ms 40416 KB Output is correct
42 Correct 166 ms 49912 KB Output is correct
43 Correct 183 ms 49656 KB Output is correct
44 Correct 192 ms 56740 KB Output is correct
45 Correct 61 ms 21624 KB Output is correct
46 Correct 152 ms 45944 KB Output is correct
47 Correct 160 ms 48376 KB Output is correct
48 Correct 95 ms 30840 KB Output is correct
49 Correct 18 ms 9336 KB Output is correct
50 Correct 191 ms 54948 KB Output is correct
51 Correct 88 ms 28928 KB Output is correct
52 Correct 93 ms 30716 KB Output is correct
53 Correct 219 ms 63352 KB Output is correct
54 Correct 65 ms 23416 KB Output is correct
55 Correct 178 ms 53752 KB Output is correct
56 Correct 203 ms 60152 KB Output is correct
57 Correct 28 ms 13688 KB Output is correct
58 Correct 160 ms 48504 KB Output is correct