CodeforcesFeb 18, 2026

Digit Game

Hazrat Ali

Codeforces

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 (1t100)  — the number of matches.

The first line of each match description contains an integer n (1n103)  — 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

Solution

#include<bits/stdc++.h>

#define ll long long
using namespace std;

int32_t main()
{
    
    ll t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;

        string s;
        cin >>s;
        if (n == 1)
        {
            if ((s[0] - '0') % 2 == 0)
            {
                cout << 2 << "\n";
            }
            else
            {
                cout << 1 << "\n";
            }
            continue;
        }
        ll even = 0;
        ll odd = 0;
        for (ll i = 0; i < s.length(); i++)
        {
            if (i % 2 == 0)
            {
                if ((s[i] - '0') % 2 == 0)
                {
                    even++;
                }

            }
            else
            {
                if ((s[i] - '0') % 2 != 0)
                {
                    odd++;
                }
            }
        }
        ll x = s.length();
        if (x % 2 == 0)
        {
            if (odd < x / 2)
            {
                cout << 2 << "\n";
            }
            else
            {
                cout << 1 << "\n";
            }
        }
        else
        {
            if (even <= ((x / 2)) )
            {
                cout << 1 << "\n";
            }
            else
            {
                cout << 2 << "\n";
            }
        }
    }
    return 0;
}





Comments