Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<cmath>
- using namespace std;
- int t;
- int main(){
- cin >> t;
- while (t--){
- int n, x; cin >> n >> x;
- string s; cin >> s; s += s[0];
- vector<int> b(2*n); b[0] = ((s[0] == '1') ? -1 : 1);
- for (int i = 1; i < n + 1; ++i)
- b[i] = b[i-1] + ((s[i] == '1') ? -1 : 1);
- int ch = b[n] - b[0];
- int c = 0;
- for (int i = 0; i < n; ++i){
- if (ch != 0) {
- if ((ch > 0) && (x - b[i] >= 0) && ((x - b[i]) % ch == 0)) c++;
- else if ((ch < 0) && (x - b[i] <= 0)) if ((b[i] - x) % ((-1)*ch) == 0) c++;
- }
- else if (b[i] == x){
- c = -1;
- cout << c << endl;
- break;
- }
- }
- if (x == 0 && c != -1) c++;
- if (c != -1)
- cout << c << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment