Pagini recente » Cod sursa (job #2633145) | Cod sursa (job #1449080) | Cod sursa (job #1588967) | Cod sursa (job #2425320) | Cod sursa (job #2535528)
#include <bits/stdc++.h>
using namespace std;
int balanceIncrement(const string &s)
{
int balance = 0;
for(char c : s)
{
if(c == '0')
balance++;
if(c == '1')
balance--;
}
return balance;
}
int main()
{
cin.tie();
ios::sync_with_stdio(false);
int t;
string s;
cin >> t;
while(t--)
{
int n, x;
cin >> n >> x;
cin >> s;
int inc = balanceIncrement(s);
if(inc == 0)
inc = 1e9;
int sol = 0;
int balance = 0;
for(char c : s)
{
if(c == '0')
balance++;
if(c == '1')
balance--;
if(inc > 1 && (x - balance) % abs(inc) == 0)
sol++;
else if(inc < -1 && (balance - x) % abs(inc) == 0)
sol++;
else if(inc == 1 && balance <= x)
sol++;
else if(inc == -1 && balance >= x)
sol++;
}
if(inc == 1e9 && x == 0)
sol = -1;
else if(x == 0)
sol++;
cout << sol << '\n';
}
return 0;
}