F. Juju and Binary String

本文解析了Codeforces F题“Juju and Binary String”的解决思路,介绍了如何通过选择非交并区间来保持字符串可爱度不变的方法,并给出了具体的实现代码。

Problem - F - Codeforces

F. Juju and Binary String

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The cuteness of a binary string is the number of 11s divided by the length of the string. For example, the cuteness of 0110101101 is 3535.

Juju has a binary string ss of length nn. She wants to choose some non-intersecting subsegments of ss such that their concatenation has length mm and it has the same cuteness as the string ss.

More specifically, she wants to find two arrays ll and rr of equal length kk such that 1≤l1≤r1<l2≤r2<…<lk≤rk≤n1≤l1≤r1<l2≤r2<…<lk≤rk≤n, and also:

  • ∑i=1k(ri−li+1)=m∑i=1k(ri−li+1)=m;
  • The cuteness of s[l1,r1]+s[l2,r2]+…+s[lk,rk]s[l1,r1]+s[l2,r2]+…+s[lk,rk] is equal to the cuteness of ss, where s[x,y]s[x,y] denotes the subsegment sxsx+1…sysxsx+1…sy, and ++ denotes string concatenation.

Juju does not like splitting the string into many parts, so she also wants to minimize the value of kk. Find the minimum value of kk such that there exist ll and rr that satisfy the constraints above or determine that it is impossible to find such ll and rr for any kk.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains two integers nn and mm (1≤m≤n≤2⋅1051≤m≤n≤2⋅105).

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, if there is no valid pair of ll and rr, print −1−1.

Otherwise, print k+1k+1 lines.

In the first line, print a number kk (1≤k≤m1≤k≤m) — the minimum number of subsegments required.

Then print kk lines, the ii-th should contain lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the range of the ii-th subsegment. Note that you should output the subsegments such that the inequality l1≤r1<l2≤r2<…<lk≤rkl1≤r1<l2≤r2<…<lk≤rk is true.

Example

input

Copy

4
4 2
0011
8 6
11000011
4 3
0101
5 5
11111

output

Copy

1
2 3
2
2 3
5 8
-1
1
1 5

Note

In the first example, the cuteness of 00110011 is the same as the cuteness of 0101.

In the second example, the cuteness of 1100001111000011 is 1212 and there is no subsegment of size 66 with the same cuteness. So we must use 22 disjoint subsegments 1010 and 00110011.

In the third example, there are 88 ways to split the string such that ∑i=1k(ri−li+1)=3∑i=1k(ri−li+1)=3 but none of them has the same cuteness as 01010101.

In the last example, we don't have to split the string.

from calendar import c
import sys
import math
#sys.stdin = open("in.txt", "r")

T = int(input())
for _ in range(T):
    n,m=[int(i) for i in input().split()]
    s=input()
    pref=[0 for i in range(n+1)]
    for i in range(n):
        pref[i+1]=pref[i]+(s[i]=='1')
    k=pref[n]
    c=k*m
    if c%n!=0:
        print("-1")
        continue
    # needed 1
    c/=n
    res=-1
    for i in range(n-m+1):
        if pref[i+m]-pref[i]==c:
            res=i
            break
    if res!=-1:
        print(1)
        print('{} {}'.format(res+1,res+m))
        continue
    
    for i in range(m+1):
        if pref[i+(n-m)]-pref[i]==k-c:
            res=i
    
    if res!=-1:
        print(2)
        print('{} {}'.format(1,res))
        print('{} {}'.format(res+1+n-m,n))
    

 考虑两种情况

1. 如果只有一段,需要区间长度和1的个数满足要求

2.不能满足要求,说明某个区间包含了太多0和1,这时反向枚举去掉的区间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值