Codeforces Round #671 (Div. 2) A.Digit Game

A.Digit Game
Description

Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play t matches of a digit game…

In each of t matches of the digit game, a positive integer is generated. It consists of n digits. The digits of this integer are numerated from 1 to n from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts.

Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins.

It can be proved, that before the end of the match (for every initial integer with n digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity.

For each of t matches find out, which agent wins, if both of them want to win and play optimally.

Input

First line of input contains an integer t (1≤t≤100) — the number of matches.

The first line of each match description contains an integer n (1≤n≤103) — the number of digits of the generated number.

The second line of each match description contains an n-digit positive integer without leading zeros.

Output

For each match print 1, if Raze wins, and 2, if Breach wins.

Example
input
4
1
2
1
3
3
102
4
2069
output
2
1
1
2
Note

In the first match no one can make a turn, the only digit left is 2, it’s even, so Breach wins.

In the second match the only digit left is 3, it’s odd, so Raze wins.

In the third match Raze can mark the last digit, after that Breach can only mark 0. 1 will be the last digit left, it’s odd, so Raze wins.

In the fourth match no matter how Raze plays, Breach can mark 9, and in the end there will be digit 0. It’s even, so Breach wins.

题意: 两个人玩游戏,Raze先开始,从一串数字中选择位于数字奇数位置的数进行标记,Breach选择偶数位置的一个数进行标记。一人一回合的进行,当只剩下最后一个数没有标记时,如果该数时偶数,则Breach获胜输出2,如果最后这个数时奇数,则Raze获胜,输出1。
输入第一行表示总共应有的测试案例数目。每个案例第一行输入一个数n,表示接下来一行输入的数应该包含n位。

题解: 很简单的一道题,只要这串数字中的奇数位出现了一次奇数,并且数字位数是奇数,那么最后一步就可以是Raze将这个奇数一直留到最后一步,如果这个数的位数是偶数,那么只要偶数位出现了一次偶数,Raze一定是必输状态,Breach可以把这个位置的偶数留到最后。详细逻辑见代码

c++ AC 代码

#include<cstdio>
#include<cstdlib>
#include<iostream>
typedef long long ll;
using namespace std;
const int MAX_N = 2e5 + 10;
char nums[MAX_N];

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		scanf("%s",nums+1);
		int odd = 0,even = 0;
		for(int i = 1;i<=n;i++)
		{
			int num = nums[i] - '0';
			if(i & 1)
			{
				if(num % 2 == 1)
					odd = 1;
			}
			else
			{
				if(num % 2 == 0)
					even = 1;
			}
		}
		if(n % 2 == 0)
		{
			if(even) puts("2");
			else puts("1");
		}
		else
		{
			if(odd) puts("1");
			else puts("2");
		}
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值