This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |