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>
#define mod 1000000007
using namespace std;
long long n,k;
string path;
string a;
string b;
long long aval=0;
long long bval=0;
long long rez=0;
bool vis[100000007];
long long power(int k)
{
if(k==0)return (long long)1;
if(k%2==0)
{
return (((power(k/2)%mod)*(power(k/2) %mod))%mod);
}
else
{
return ((((long long)2*(power(k/2)%mod)%mod)*(power(k/2)%mod))%mod);
}
}
void solve(long long start,long long kk,long long nn,bool chk)
{
if(nn==path.size())
{
if(kk==0 && !vis[start] && start>=aval && start<=bval)
{
rez+=start;
rez%=1000000007;
vis[start]=1;
//cout << start << endl;
}
return;
}
if(kk==0)
{
if(!chk)
{
if(path[nn]=='L')solve(2*start,kk,nn+1,chk);
else
{
solve(2*start+1,kk,nn+1,chk);
}
}
else
{
if(path[nn]=='R')solve(2*start,kk,nn+1,chk);
else
{
solve(2*start+1,kk,nn+1,chk);
}
}
}
else
{
if(!chk)
{
if(path[nn]=='L')
{
solve(2*start,kk,nn+1,chk);
solve(2*start+1,kk-1,nn+1,chk^1);
}
else
{
solve(2*start+1,kk,nn+1,chk);
solve(2*start,kk-1,nn+1,chk^1);
}
}
else
{
if(path[nn]=='R')
{
solve(2*start,kk,nn+1,chk);
solve(2*start+1,kk-1,nn+1,chk^1);
}
else
{
solve(2*start+1,kk,nn+1,chk);
solve(2*start,kk-1,nn+1,chk^1);
}
}
}
}
int main()
{
cin >> n >> k;
cin >> path;
cin >> a;
cin >> b;
if(k>0)
{
long long br=0;
for(long long i=a.size()-1;i>=0;i--)
{
if(a[i]=='1')aval+=(1<<br);
br++;
}
br=0;
for(long long i=b.size()-1;i>=0;i--)
{
if(b[i]=='1')bval+=(1<<br);
br++;
}
solve(1,k,0,0);
solve(1,k,0,1);
cout << rez;
}
else
{
long long av=0;
long long bv=0;
long long br=0;
for(long long i=a.size()-1;i>=0;i--)
{
if(a[i]=='1')
{
av+=power(br);
av%=mod;
}
br++;
}
br=0;
for(long long i=b.size()-1;i>=0;i--)
{
if(b[i]=='1')
{
bv+=power(br);
bv%=mod;
}
br++;
}
if(av==bv)
{
cout << av%mod;
return 0;
}
//cout << av << ' ' << bv<<endl;
cout << (av%mod+bv%mod)%mod;
}
return 0;
}
Compilation message (stderr)
ljetopica.cpp: In function 'void solve(long long int, long long int, long long int, bool)':
ljetopica.cpp:26:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(nn==path.size())
~~^~~~~~~~~~~~~
# | 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... |