Ticket Game【博弈】

题目

Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n 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 n2 digits of this ticket is equal to the sum of the last n2 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 0 to 9. 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 n (2≤n≤2⋅105) — the number of digits in the ticket.

The second line contains a string of n digits and "?" characters — the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-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

4
0523

Output

Bicarp

Input

2
??

Output

Bicarp

Input

8
?054??0?

Output

Bicarp

Input

6
???00?

Output

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.

题意

一张票有n位数,如果这张票的前一半数字的和等于后一半数字的和(n一定是偶数),就称这张票为快乐票。有些数被擦除了,标记为’?’(’?‘的个数也是偶数),现在Monocarp 和 Bicarp 进行一个游戏,两人轮流将’?'变换成0到9的任意一个数,Monocarp先手,如果最后票为快乐票则Bicarp赢,否则Monocarp赢。

分析

分情况讨论:假设所有的数左半部分和为sum1,右边为sum2 左边有n个?,右边有m个

①sum1==sum2:
当m不等于n时,先手一定赢。反之则后者赢。
②假如sum1>sum2
(1)n>=m时,先手可以在一边一直放9,后手弥补不了差距。先手必胜。 (2)n<m时,前2n次同上,剩下的次数中,先手放sum1时,后手只能是放sum2来使得sum1+sum2=9;所以当9|sum1-sum2时后手胜。不然的话后手是必输的。

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=2e5+5;
 5 int n;
 6 char s[maxn];
 7 int ls, rs, x, y;
 8 
 9 int main(){
10     scanf("%d",&n);
11     scanf("%s",s+1);
12     for(int i=1;i<=n/2;i++){
13         if(s[i]=='?')x++;
14         else ls+=s[i]-'0';
15     }
16     for(int i=n/2+1;i<=n;i++){
17         if(s[i]=='?')y++;
18         else rs+=s[i]-'0';
19     }
20     if(ls<rs){
21         swap(ls, rs); swap(x, y);
22     }
23     if(ls==rs){
24         printf("%s\n", x==y? "Bicarp":"Monocarp");
25     }
26     else{
27         if(x>=y) printf("Monocarp\n");
28         else printf("%s\n", (y-x)%2==0 && ls-rs==(y-x)/2*9? "Bicarp":"Monocarp");
29     }
30     return 0;
31 }

 

posted @ 2020-04-24 12:50  Vocanda  阅读(308)  评论(0编辑  收藏  举报