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

Infinite Prefixes

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Calculate the number of prefixes of tt with balance equal to xx. The balance of some string qq is equal to cnt0,q−cnt1,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 (1≤T≤100) — the number of test cases.

Next 2T lines contain descriptions of test cases — two lines per test case. The first line contains two integers n and x (Educational Codeforces Round 81 (Rated for Div. 2)  B - Infinite Prefixes ) — the length of string s and the desired balance, respectively.

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

It's guaranteed that the total sum of nn doesn't exceed Educational Codeforces Round 81 (Rated for Div. 2)  B - Infinite Prefixes.

 

 

Output

 

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

 

 

Example

 

 

input

4
6 10
010010
5 3
10101
1 0
0
2 0
01

output

3
0
1
-1

 

 

Note

 

In the first test case, there are 3 good prefixes of t: with length 28, 30 and 32.

题目大意


给一个长度已知可以无限重复的01串s,定义一个字符串的q值为’0’字符的个数减去’1’字符的个数,问给定字符串s的所有前缀中(包括空字符串)q值为x的个数

题目不难,但是细节比较麻烦,无非就是一个周期问题,但是一个周期之内会有极大极小,就可能导致多解,首先预处理出串的每一个位置的q值,然后遍历一遍01串,如果(x-该位置的q值)%(01串最后一个位置q值)=0,还没完,因为你得检验如果该位置+(x-该位置的q值)/(01串最后一个位置q值)>0就ok,完全可以,当然想到这还不够,,还有无限解的情况

何时解无穷?

01串最后一个位置q值=0: 如果x=0,那么一定有无穷解;

                                            如果xEducational Codeforces Round 81 (Rated for Div. 2)  B - Infinite Prefixes0,那么遍历一遍字符串,要是某一个位置q值=x,则也有无穷解

当然x=0的时候一定有解,因为空前缀也行~

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
ll a[100005];
int main()
{
    ll t;
    cin>>t;
    for(rg i=1;i<=t;i++)
    {
        ll n,x,ans=0;
        cin>>n>>x;
        string s;
        cin>>s;
        s[0]=='0'?a[0]=1:a[0]=-1;
        for(rg i=1;s[i];i++)
        {
            a[i]=a[i-1];
            if(s[i]=='0')a[i]++;
            else a[i]--;
        }
        if(!x&&!a[n-1])
        {
            cout<<-1<<endl;
            continue;
        }
        if(!x)ans++;
        if(!a[n-1])
        {
            ll tep=0,flag=0;
            for(rg i=0;i<n;i++)
            {
                if(a[i]==x)
                {
                    cout<<-1<<endl;
                    flag=1;
                    break;
                }
            }
            if(!flag)cout<<0<<endl;
            continue;
        }
        for(rg i=0;i<n;i++)
        {
            if((x-a[i])%a[n-1]==0&&i+1+(x-a[i])/a[n-1]*n>0)ans++;
        }
        cout<<ans<<endl;
    }
    while(1)getchar();
    return 0;
}

 

 

Educational Codeforces Round 81 (Rated for Div. 2)  B - Infinite PrefixesEducational Codeforces Round 81 (Rated for Div. 2)  B - Infinite Prefixes 菱形继承 发布了214 篇原创文章 · 获赞 137 · 访问量 20万+ 私信 关注
上一篇:关于Queries_per_sec 性能计数器


下一篇:Jmeter吞吐量控制器 Thoutghput Controller