crowulll

Untitled

Aug 17th, 2020 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<cmath>
  4. using namespace std;
  5. int t;
  6. int main(){
  7.     cin >> t;
  8.     while (t--){
  9.         int n, x; cin >> n >> x;
  10.         string s; cin >> s; s += s[0];
  11.         vector<int> b(2*n); b[0] = ((s[0] == '1') ? -1 : 1);
  12.         for (int i = 1; i < n + 1; ++i)
  13.             b[i] = b[i-1] + ((s[i] == '1') ? -1 : 1);
  14.         int ch = b[n] - b[0];
  15.         int c = 0;
  16.         for (int i = 0; i < n; ++i){
  17.             if (ch != 0) {
  18.                     if ((ch > 0) && (x - b[i] >= 0) && ((x - b[i]) % ch == 0)) c++;
  19.                     else if ((ch < 0) && (x - b[i] <= 0)) if ((b[i] - x) % ((-1)*ch) == 0) c++;
  20.             }
  21.             else if (b[i] == x){
  22.                 c = -1;
  23.                 cout << c << endl;
  24.                 break;
  25.             }
  26.         }
  27.         if (x == 0 && c != -1) c++;
  28.         if (c != -1)
  29.             cout << c << endl;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment