Codeforces 1562 C. Rings

本场比赛其他题目的题解

A. The Miracle and the Sleeper
B. Scenes From a Memory
C. Rings
D1. Two Hundred Twenty One (easy version)
D2. Two Hundred Twenty One (hard version)

C. Rings

开启传送门

题目描述

Frodo was caught by Saruman. He tore a pouch from Frodo’s neck, shook out its contents —there was a pile of different rings: gold and silver…

“How am I to tell which is the One?!” the mage howled.

“Throw them one by one into the Cracks of Doom and watch when Mordor falls!”

Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found n n n rings. And the i i i-th ring was either gold or silver. For convenience Saruman wrote down a binary string s s s of n n n characters, where the i i i-th character was 0 if the i i i-th ring was gold, and 1 if it was silver.

Saruman has a magic function f f f, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, f ( 001010 ) = 10 , f ( 111 ) = 7 , f ( 11011101 ) = 221 f(001010) = 10, f(111) = 7, f(11011101) = 221 f(001010)=10,f(111)=7,f(11011101)=221.

Saruman, however, thinks that the order of the rings plays some important role. He wants to find 2 2 2 pairs of integers ( l 1 , r 1 ) , ( l 2 , r 2 ) (l_1, r_1), (l_2, r_2) (l1,r1),(l2,r2), such that:

  • 1 ≤ l 1 ≤ n 1 \le l_1 \le n 1l1n, 1 ≤ r 1 ≤ n 1 \le r_1 \le n 1r1n, r 1 − l 1 + 1 ≥ ⌊ n 2 ⌋ r_1-l_1+1\ge \lfloor \frac{n}{2} \rfloor r1l1+12n

  • 1 ≤ l 2 ≤ n 1 \le l_2 \le n 1l2n, 1 ≤ r 2 ≤ n 1 \le r_2 \le n 1r2n, r 2 − l 2 + 1 ≥ ⌊ n 2 ⌋ r_2-l_2+1\ge \lfloor \frac{n}{2} \rfloor r2l2+12n

  • Pairs ( l 1 , r 1 ) (l_1, r_1) (l1,r1) and ( l 2 , r 2 ) (l_2, r_2) (l2,r2) are distinct. That is, at least one of l 1 ≠ l 2 l_1 \neq l_2 l1=l2 and r 1 ≠ r 2 r_1 \neq r_2 r1=r2 must hold.

  • Let t t t be the substring s [ l 1 : r 1 ] s[l_1:r_1] s[l1:r1] of s s s, and w w w be the substring s [ l 2 : r 2 ] s[l_2:r_2] s[l2:r2] of s s s. Then there exists non-negative integer k k k, such that f ( t ) = f ( w ) ⋅ k f(t) = f(w) \cdot k f(t)=f(w)k.

Here substring s [ l : r ] s[l:r] s[l:r] denotes s l s l + 1 … s r − 1 s r s_ls_{l+1}\ldots s_{r-1}s_r slsl+1sr1sr, and ⌊ x ⌋ \lfloor x \rfloor x denotes rounding the number down to the nearest integer.

Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.

Input

Each test contains multiple test cases.

The first line contains one positive integer t t t ( 1 ≤ t ≤ 1 0 3 1 \le t \le 10^3 1t103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 4 2 \le n \le 2 \cdot 10^4 2n2104) — length of the string.

The second line of each test case contains a non-empty binary string of length n n n.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For every test case print four integers l 1 l_1 l1, r 1 r_1 r1, l 2 l_2 l2, r 2 r_2 r2, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively.

If there are multiple solutions, print any.

Example

Input1
7
6
101111
9
111000111
8
10000000
5
11011
6
001111
3
101
30
100000000000000100000000000000
Output1
3 6 1 3
1 9 4 9
5 8 1 4
1 5 3 5
1 6 2 4
1 2 2 3
1 15 16 30

Note

In the first testcase f ( t ) = f ( 1111 ) = 15 f(t) = f(1111) = 15 f(t)=f(1111)=15, f ( w ) = f ( 101 ) = 5 f(w) = f(101) = 5 f(w)=f(101)=5.

In the second testcase f ( t ) = f ( 111000111 ) = 455 f(t) = f(111000111) = 455 f(t)=f(111000111)=455, f ( w ) = f ( 000111 ) = 7 f(w) = f(000111) = 7 f(w)=f(000111)=7.

In the third testcase f ( t ) = f ( 0000 ) = 0 f(t) = f(0000) = 0 f(t)=f(0000)=0, f ( w ) = f ( 1000 ) = 8 f(w) = f(1000) = 8 f(w)=f(1000)=8.

In the fourth testcase f ( t ) = f ( 11011 ) = 27 f(t) = f(11011) = 27 f(t)=f(11011)=27, f ( w ) = f ( 011 ) = 3 f(w) = f(011) = 3 f(w)=f(011)=3.

In the fifth testcase f ( t ) = f ( 001111 ) = 15 f(t) = f(001111) = 15 f(t)=f(001111)=15, f ( w ) = f ( 011 ) = 3 f(w) = f(011) = 3 f(w)=f(011)=3.

题意

t t t 次询问。每次给你一个长度为 n n n 的仅包含 0 0 0 1 1 1 的字符串,让你找到这个字符串的两个不同的长度至少为 ⌊ n 2 ⌋ \lfloor \frac{n}{2} \rfloor 2n 的子串,并且这两个子串转换成二进制数的时候是倍数关系。子串可以有前导 0 0 0 。输出这两个子串的起止坐标。

分析

比较容易想的是:

  1. 一倍关系,也即两个串长得一样。
  2. 二倍关系,也即后一个串多一个后缀 0 0 0

基于上述想法,可以分两种情况讨论。

  1. 原串只有 1 1 1,这种情况,我们可以找两个完全一样的串,分别是 [ 1 , n − 1 ] , [ 2 , n ] [1, n-1], [2, n] [1,n1],[2,n]
  2. 原串存在 0 0 0

上述的情况 2 2 2,又可以分为两种情况。我们不妨设第一个 0 0 0 出现的位置是 i n d ind ind

  1. i n d < ⌊ n 2 ⌋ ind \lt \lfloor \frac{n}{2} \rfloor ind<2n ,这种情况,我们就找一倍关系,然后多一个前导 0 0 0,也即两个串分别是 [ i n d , n ] , [ i n d + 1 , n ] [ind, n], [ind+1, n] [ind,n],[ind+1,n]
  2. i n d ≥ ⌊ n 2 ⌋ ind \ge \lfloor \frac{n}{2} \rfloor ind2n ,这种情况,我们就找二倍关系,然后多一个后缀 0 0 0,也即两个串分别是 [ 1 , i n d ] , [ 1 , i n d + 1 ] [1, ind], [1, ind+1] [1,ind],[1,ind+1]
#include<bits/stdc++.h>
using namespace std;
int n;
string a;
void solve() {
	cin>>n>>a;
	int ind = -1;
	for(int i = 0;i<a.length();i++) {
		if(a[i] == '0') {
			ind = i;
			break;
		}
	}
	if(ind==-1) {
		cout<<1<<' '<<n-1<<' '<<2<<' '<<n<<endl;
		return ;
	}
	if(n-ind-1>=n/2) {
		cout<<ind+1<<' '<<n<<' '<<ind+2<<' '<<n<<endl;
	} else {
		assert(ind>=n/2);
		cout<<1<<' '<<ind+1<<' '<<1<<' '<<ind<<endl;
	}
}
int main() {
	int t;
	cin>>t;
	while(t--) solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值