D. Ticket Game

本文解析了一个基于票面数字的游戏博弈论问题,通过分析票面上已知数字和问号位置,判断两个玩家Monocarp和Bicarp在最优策略下谁将赢得游戏。涉及算法包括动态规划和数学计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://codeforces.com/contest/1215/problem/D

Monocarp and Bicarp live in Berland, where every bus ticket consists of nn digits (nn is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.

Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n2n2 digits of this ticket is equal to the sum of the last n2n2 digits.

Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 00 to 99. The game ends when there are no erased digits in the ticket.

If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.

Input

The first line contains one even integer nn (2≤n≤2⋅105)(2≤n≤2⋅105) — the number of digits in the ticket.

The second line contains a string of nn digits and "?" characters — the ticket which Monocarp and Bicarp have found. If the ii-th character is "?", then the ii-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.

Output

If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).

Examples

input

Copy

4
0523

output

Copy

Bicarp

input

Copy

2
??

output

Copy

Bicarp

input

Copy

8
?054??0?

output

Copy

Bicarp

input

Copy

6
???00?

output

Copy

Monocarp

Note

Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.

In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.

思路:

博弈题,可以简化成一个博弈模型

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int maxn=1e6+5;
long long n,s1=0,s2=0,b1=0,b2=0;
char a[200001];
int main()
{
	cin>>n;
	cin>>a;
	for(int i=0;i<=n/2-1;i++)
	{
		if(a[i]=='?')
		b1++;
		else
		s1+=a[i]-'0';
	}
	for(int i=n/2;i<n;i++)
	{
		if(a[i]=='?')
		b2++;
		else
		s2+=a[i]-'0';
	}
	if(b1==0&&b2==0)
	{
		if(s1==s2)
		cout<<"Bicarp";
		else
		cout<<"Monocarp";
	}
	else if(b1==b2)
	{
		if(fabs(s1-s2)>=1)
		cout<<"Monocarp";
		else
		cout<<"Bicarp";
	}
	else if(b1>b2)
	{
			if(s2-s1==9*(b1-b2)/2)
			cout<<"Bicarp";
			else
			cout<<"Monocarp";
	}
	else if(b1<b2)
	{
		if(s1-s2==9*(b2-b1)/2)
		cout<<"Bicarp";
		else
		cout<<"Monocarp";
	}
	
	
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值