前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Educational Codeforces Round 81 (Rated for Div. 2) B - Infinite Prefixes

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

作者头像
glm233
发布2020-09-28 11:15:33
3640
发布2020-09-28 11:15:33
举报

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 (

1\leq n\leq 10^5,-10^9\leq x\leq 10^9
1\leq n\leq 10^5,-10^9\leq x\leq 10^9

) — 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

10^5
10^5

.

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

代码语言:javascript
复制
4
6 10
010010
5 3
10101
1 0
0
2 0
01

output

代码语言:javascript
复制
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,那么一定有无穷解;

如果x

\neq
\neq

0,那么遍历一遍字符串,要是某一个位置q值=x,则也有无穷解

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档