# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
233342 | 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, A, B, cur;
vector <int> f;
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, int l, int r, int tl, int tr)
{
if (l > tr || r < tl) return;
if (l == r)
{
cur = sm(cur, (f[k] * 1ll * (l + (1 << (n - 1)) - 1)) % M);
return;
}
vector <int> fl, fr;
fl.resize(27);
fr.resize(27);
if (s[i] == 'L')
{
for (int j = 0; j <= k; j++)
{
fl[j] = sm(fl[j], f[j]);
fr[j + 1] = sm(fr[j + 1], f[j]);
}
}
else
{
for (int j = 0; j <= k; j++)
{
fl[j + 1] = sm(fl[j + 1], f[j]);
fr[j] = sm(fr[j], f[j]);
}
}
int md = (l + r) >> 1;
f = fl;
if (md >= tl) dfs(v * 2, i + 1, l, md, tl, tr);
f = fr;
if (md + 1 <= tr) dfs(v * 2 + 1, i + 1, md + 1, r, tl, tr);
}
ll calc(int revers)
{
if (revers)
{
for (i = 0; i < n; i++)
{
if (s[i] == 'L') s[i] = 'R';
else s[i] = 'L';
}
}
A = 0;
for (i = 0; i < n; i++)
if (a[i] == '1') A += bp(2, i);
B = -0;
for (i = 0; i < n; i++)
if (b[i] == '1') B += bp(2, i);
A -= bp(2, n - 1);
B -= bp(2, n - 1);
A++;
B++;
f.resize(27);
for (i = 0; i < 27; i++) f[i] = 0;
cur = 0;
f[0] = 1;
dfs(1, 0, 1, (1 << (n - 1)), A, B, f);
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;
}