# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
233293 | kartel | Ljetopica (COI19_ljetopica) | C++14 | 0 ms | 0 KiB |
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 +100500
#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;
string s, a, b;
int n, k, i;
ll ans = 0;
ll f[(1 << 26)][30];
ll sm(ll x, ll y) {return (x + y) % M;}
ll bp(ll x, ll y)
{
if (y == 0) return 1ll;
if (y % 2) return (bp(x, y - 1) * x) % M;
ll a = bp(x, y / 2);
return (a * a) % M;
}
void dfs(int v, int i)
{
if (i == n - 1) return;
if (s[i] == 'L')
{
for (int j = 0; j < k; j++)
{
f[v * 2][j] = sm(f[v * 2][j], f[v][j]);
f[v * 2 + 1][j + 1] = sm(f[v * 2 + 1][j + 1], f[v][j]);
}
}
else
{
for (int j = 0; j < k; j++)
{
f[v * 2][j + 1] = sm(f[v * 2][j + 1], f[v][j]);
f[v * 2 + 1][j] = sm(f[v * 2 + 1][j], f[v][j]);
}
}
dfs(v * 2, i + 1);
dfs(v * 2 + 1, i + 1);
}
ll calc(int revers)
{
if (revers)
{
for (i = 0; i < n; i++)
{
if (s[i] == 'L') s[i] = 'R';
else s[i] = 'L';
}
}
memset(f, 0, sizeof(f));
f[1][0] = 1;
f[1][1] = 1;
dfs(1, 0);
int A = 0;
for (i = 0; i < n; i++)
if (a[i] == '1') A += bp(2, i);
int B = -0;
for (i = 0; i < n; i++)
if (b[i] == '1') B += bp(2, i);
ll cur = 0;
for (i = A; i <= B; i++) cur = sm(cur, f[i][k] * i);
return cur;
}
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");
cin >> n >> k;
cin >> s;
cin >> a;
cin >> b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if (k == 0)
{
bitset <1005> bt;
for (i = 0; i <= n; i++) bt[i] = 0;
bt[0] = 1;
i = 0;
while (i < n - 1)
{
if (s[i] == 'L')
{
bitset <1005> nw;
for (int i = 1; i <= n; i++) nw[i] = bt[i - 1];
bt = nw;
}
else
{
bitset <1005> nw;
nw[0] = 1;
for (int i = 1; i <= n; i++) nw[i] = bt[i - 1];
bt = nw;
}
i++;
}
i = n - 1;
while (i >= 0 && bt[i] == a[i] - '0') i--;
// cerr << i << el;
if (i < 0 || a[i] == '0')
{
i = n - 1;
while (i >= 0 && bt[i] == b[i] - '0') i--;
if (i < 0 || b[i] == '1')
{
i = 0;
for (i = 0; i < n; i++)
{
if (bt[i]) ans = sm(ans, bp(2, i));
}
}
}
for (i = 0; i <= n; i++) bt[i] = 0;
bt[0] = 1;
i = 0;
while (i < n - 1)
{
if (s[i] == 'R')
{
bitset <1005> nw;
for (int i = 1; i <= n; i++) nw[i] = bt[i - 1];
bt = nw;
}
else
{
bitset <1005> nw;
nw[0] = 1;
for (int i = 1; i <= n; i++) nw[i] = bt[i - 1];
bt = nw;
}
i++;
}
// for (int i = 0; i < n; i++) cout << bt[i];
// cout << el;
// for (int i = 0; i < n; i++) cout << b[i];
// cout << el;
// cout << ans << el;
i = n - 1;
while (i >= 0 && bt[i] == a[i] - '0') i--;
if (i < 0 || a[i] == '0')
{
i = n - 1;
while (i >= 0 && bt[i] == b[i] - '0') i--;
if (i < 0 || b[i] == '1')
{
i = 0;
for (i = 0; i < n; i++)
{
if (bt[i]) ans = sm(ans, bp(2, i));
}
}
}
}
else ans = sm(calc(1), calc(0));
cout << ans % M;
}