Problem Author: | De3b0o |
Prepared by: | De3b0o |
Edited by: | Hasan Saleh (MathModel) |
Solution
For a string and an integer x \ge 1, if the string already has or more equal consecutive characters then the answer is either or . if the first equal consecutive characters are zeros and otherwise.
Otherwise if doesn't have or more equal consecutive characters. We only care about the maximum equal suffix.
Let's say the suffix's length is and assuming that the characters are ones and not zeros (If they were zeros the answer is equal to the answer if they were ones). The answer is \frac{1}{2^{x-y}} + (\frac{2^{x-y}-1}{2^{x-y}})(\frac{2^{x-1}-1}{2^x-1}).
Proof:We have two possibilities:
The first is that the first x-y random characters are equal to , the probability of that happening is \frac{1}{2^{x-y}} and the probability of the last character being is (because then we would have y+x-y=x consecutive ones and the process will end immediately).
The second is that at least one of the first x-y characters are not equal to (it does not matter which ones), the probability of that happening is \frac{2^{x-y}-1}{2^{x-y}} ( the probability of them all being ) and the probability of the last character being is \frac{2^{x-1}-1}{2^x-1}.
So the answer must be:
(The probability of the first case)(The probability of ending up with if it happened) + (The probability of the second case)(The probability of ending up with if it happened)
Which is equal to the formula above.
Now we'll explain why the probability of ending up with is equal to \frac{2^{x-1}-1}{2^x-1} in the second case.
It's the same as the probability of ending up with starting with s = 0 (because after adding the first the new maximum equal suffix is the last character which is ).
Let's call the probability of ending up with starting with s=0.
Then:
p = z + (1-z)(1-p), where z = \frac{1}{2^{x-1}}
p = z + (1-z)(1-p) = z + 1 - z - p +z \cdot p
(2-z)\cdot p = 1
p = \frac{1}{2-z} = \frac{1}{2-\frac{1}{2^{x-1}}}
We can multiply the numerator and the denominator by 2^{x-1}
p = \frac{2^{x-1}}{2^x - 1}
And the probability of ending up with starting with s=0 instead of ending up with is 1-p = \frac{2^x - 1 - 2^{x-1}}{2^x - 1} = \frac{2^{x-1} - 1}{2^x - 1}.
Complexity \mathcal{O}(q \cdot \log(\max(x))).
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "YES" << "\n";
#define no cout << "NO" << "\n";
#define pll pair<ll, ll>
#define lin cout << "\n";
#define mid ((l + r) / 2)
#define lc (2 * x)
#define rc (2 * x + 1)
#define double long double
using namespace std;
const ll N = 5e5+9 , m = 1e9+7;
ll fp(ll x , ll y)
{
if(y==0)
return 1;
ll z = fp(x,y/2);
if(y%2)
return z*z%m*x%m;
else
return z*z%m;
}
ll q,ans,pro,z,x,p,d,zz;
char c,pr;
char e[N];
ll mod(ll x)
{
x%=m;
x+=m;
x%=m;
return x;
}
int main()
{
d3
ll T = 1;
cin >> T;
while(T--)
{
cin >> q;
pr='0';
p=0;
for(int i = 1 ; q+10>=i ; i++)
e[i]='2';
for(int qq = 1 ; q>=qq ; qq++)
{
cin >> c >> x;
if(c==pr)
p++;
else
pr=c,p=1;
if(e[p]=='2')
e[p]=c;
if(x<=q+10&&e[x]!='2')
{
if(e[x]=='1')
cout << 1 << '\n';
else
cout << 0 << '\n';
continue;
}
pro = mod(fp(2,x-1)-1);
d = mod(fp(2,x)-1);
pro=mod(pro*fp(d,m-2));
z=fp(fp(2,x-p),m-2);
zz = mod(1 - z);
ans = mod(z + zz*pro);
if(c=='0')
ans=mod(1-ans);
cout << ans << '\n';
}
}
}