Infinite Prefixes CodeForces - 1295B

Posted hulian425

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Infinite Prefixes CodeForces - 1295B相关的知识,希望对你有一定的参考价值。

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given string ss of length nn consisting of 0-s and 1-s. You build an infinite string tt as a concatenation of an infinite number of strings ss, or t=sssst=ssss… For example, if s=s= 10010, then t=t= 100101001010010...

Calculate the number of prefixes of tt with balance equal to xx. The balance of some string qq is equal to cnt0,qcnt1,qcnt0,q−cnt1,q, where cnt0,qcnt0,q is the number of occurrences of 0 in qq, and cnt1,qcnt1,q is the number of occurrences of 1 in qq. The number of such prefixes can be infinite; if it is so, you must say that.

A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd".

Input

The first line contains the single integer TT (1T1001≤T≤100) — the number of test cases.

Next 2T2T lines contain descriptions of test cases — two lines per test case. The first line contains two integers nn and xx (1n1051≤n≤105, 109x109−109≤x≤109) — the length of string ss and the desired balance, respectively.

The second line contains the binary string ss (|s|=n|s|=n, si{0,1}si∈{0,1}).

It‘s guaranteed that the total sum of nn doesn‘t exceed 105105.

Output

Print TT integers — one per test case. For each test case print the number of prefixes or 1−1 if there is an infinite number of such prefixes.

Example
input
Copy
4
6 10
010010
5 3
10101
1 0
0
2 0
01
output
Copy
3
0
1
-1
Note

In the first test case, there are 3 good prefixes of tt: with length 2828, 3030 and 3232.

 
题解: 前缀和记录字符串每个位置的平衡值。 注意平衡值x可能是负的
#include<iostream>
#include<string>
#include <cstdlib>

#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include <iomanip>

// #pragma comment(linker, "/STACK:1024000000,1024000000")
// #define pi acos(-1)
// #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x7f7f7f7f //2139062143
#define INF1 0x3f3f3f3f //1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.141592653589793//23846264338327950254
#define pb push_back
#define ll long long
#define debug cout << "debug
";
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
#define CNM ios::sync_with_stdio(false);cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m,n) for (int i = m;  i < n; i++)

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<0||ch>9){if(ch==-)w=-1;ch=getchar();}
   while(ch>=0&&ch<=9) s=s*10+ch-0,ch=getchar();
   return s*w;
}
#define local
#ifdef local
#endif

#define MAX 10233
#define LCH(i) ((i) << 1)
#define RCH(i) ((i) << 1 | 1)
const int N = 1e5 + 5;
ll a[N] = {0};
int main()
{
    freopen("in.txt","r",stdin);
    CNM;
    ll t;
    cin >> t;
    while (t--)
    {
        ll n, v;
        cin >> n >> v;
        string s;
        cin >> s;
        ll ans = 0;
        for (int i = 0; i < n; i++)
        {
            a[i + 1] = a[i] + (s[i] == 0?1:-1);
        }
        for (int i = 0; i < n; i++)
        {
            int dv = v - a[i];
            if (a[n] == 0)
            {
                if (dv == 0)
                    ans = -1;
            }
            else if (dv % a[n] == 0)
            {
                dv /= a[n];
                if (dv >= 0) ans++;
            }
        }
        printf("%lld
", ans);
    }
}

 

 

以上是关于Infinite Prefixes CodeForces - 1295B的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1295B - Infinite Prefixes

Codeforces 1295B Infinite Prefixes

Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

bigdata hadoop java codefor wordcount 修改

错误代码:1066。不是唯一的表/别名:'circle_call_prefixes'

trie树POJ2001Shortest Prefixes