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 <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +1005
#define MaxS N * N
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
ll f[N][N][2] {0};
int lss, len, n, i, z, k;
ll sm(ll x, ll y) {return (x + y) % M;}
ll mult(ll x, ll y) {return (x * y) % M;}
ll bp(ll x, ll y)
{
if (y == 0) return 1ll;
if (y % 2) return mult(bp(x, y - 1), x);
ll a = bp(x, y / 2);
return mult(a, a);
}
string dec(string x)
{
int i = n - 1;
while (i >= 0 && x[i] == '0') x[i] = '1', i--;
x[i] = '0';
return x;
}
ll calc(string x, string y)
{
memset(f, 0, sizeof(f));
f[1][0][0] = bp(2, n - 1);
for (len = 1; len < n; len++)
for (z = 0; z <= k; z++)
for (lss = 0; lss < 2; lss++)
{
if (f[len][z][lss] == 0) continue;
if (!lss)
{
if (x[len] == y[len])
{
if (x[len] == '1')
{
f[len + 1][z][lss] = sm(f[len + 1][z][lss], bp(2, n - len - 1));
f[len + 1][z + 1][1] = sm(f[len + 1][z + 1][1], f[len][z][lss]);
}
f[len + 1][z][lss] = sm(f[len + 1][z][lss], f[len][z][lss]);
}
else if (x[len] < y[len])
{
f[len + 1][z + 1][lss] = sm(f[len + 1][z + 1][lss], sm(bp(2, n - len - 1), f[len][z][lss]));
f[len + 1][z][1] = sm(f[len + 1][z][1], f[len][z][lss]);
}
else f[len + 1][z + 1][lss] = sm(f[len + 1][z + 1][lss], f[len][z][lss]);
}
else
{
if (x[len] == '0')
{
f[len + 1][z][lss] = sm(f[len + 1][z][lss], f[len][z][lss]);
f[len + 1][z + 1][lss] = sm(f[len + 1][z + 1][lss], sm(f[len][z][lss], bp(2, n - len - 1)));
}
else
{
f[len + 1][z + 1][lss] = sm(f[len + 1][z + 1][lss], f[len][z][lss]);
f[len + 1][z][lss] = sm(f[len + 1][z][lss], sm(f[len][z][lss], bp(2, n - len - 1)));
}
}
}
return sm(f[n][k][0], f[n][k][1]);
}
int main()
{
srand(time(0));
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
// in("input.txt");
// out("output.txt");
string x, l, r, s;
cin >> n >> k;
cin >> s;
cin >> l;
cin >> r;
x = "1";
for (i = 0; i < n - 1; i++) if (s[i] == 'L') x += "0"; else x += "1";
ll cur1 = calc(x, r);
ll cur2 = calc(x, dec(l));
x = "1";
for (i = 0; i < n - 1; i++) if (s[i] == 'R') x += "0"; else x += "1";
cur1 = sm(cur1, calc(x, r));
cur2 = sm(cur2, calc(x, dec(l)));
// cout << cur2 << el;
cout << (cur1 - cur2 + M) % M;
}
# | 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... |