• B. Infinite Prefixes


    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.

     

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 100007;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            ll n, x;
            cin >> n >> x;
            string s;
            cin >> s;
            vector<ll> a(n+1,0);
            ll res=0;
            if (x == 0)
                res++;
            for (int i = 0; i < s.size(); i++)
            {
                if (s[i] == '0')
                {
                    a[i + 1] = a[i] + 1;
                }
                else
                {
                    a[i + 1] = a[i] - 1;
                }
                if (a[i + 1] == x)
                    res++;
            }
            if (a[n] == 0 && res > 0)
            {
                cout << -1 << endl;
                continue;
            }
            else
            {
                if (a[n] == 0)
                {
                    cout << res << endl;
                    continue;
                }
                rep(i, 1, n)
                {
                    if ((x-a[i])% a[n]==0 && (x - a[i]) / a[n] > 0)
                        res++;
                }
                cout << res << endl;
            }
        }
        return 0;
    }

     

  • 相关阅读:
    Android应用程序与SurfaceFlinger服务的关系概述和学习计划【转】
    Linux内核的LED设备驱动框架【转】
    电源管理-4种休眠方式状态
    linux 管道,输出重定向,后端执行
    find 和grep的区别
    linux启动脚本
    linux启动介绍
    sudo的使用
    ps aux|grep *** 解释
    php图片防盗链
  • 原文地址:https://www.cnblogs.com/dealer/p/12782816.html
Copyright © 2020-2023  润新知