欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ticket Game CodeForces - 1215D(博弈题,巴什博弈思维)

程序员文章站 2022-03-16 08:26:07
...

题意:两个人玩游戏,通过轮流填数字(0~9),若最终左右两边的和相等,后手赢,否则先手赢。起始有部分数字和空格。

官方题解:

Ticket Game CodeForces - 1215D(博弈题,巴什博弈思维)

题解翻译:

让我们把余额表示为左半部分数字和右半部分数字和的差。也让我成为最小可能的余额(如果我们用00的左半部替换所有的问号和99的右半部分中的所有问号都可以计算),让RR成为最大可能的平衡。

当且仅当l+r2=0l+r2=0时,第二个玩家获胜。我们就票子上剩下的问号做归纳证明吧。

如果所有的字符都是数字,那么第二个玩家只有在票子满意的情况下才能获胜,也就是当L+R2=0L+R2=0时。

好吧,现在假设问号是偶数,现在轮到第一个玩家了。每转一圈,R-LR-L的值减小99,并且可以将LL设置为从当前LL到L+9L+9的任意数字。如果l+r>0l+r>0,那么第一个玩家可以使ll尽可能大,并将其设置为l+9l+9。第二个玩家在回合中能做的最好的事情是将r r设置为r-9r-9(保持l l不变),l+rl+r的值将与前两回合相同。L+R<0L+R<0的情况也可作类似分析。在l+r=0l+r=0的情况下,第二个玩家有一个对称的策略。

我的理解:类似于巴什博弈,考虑后手赢的情况,其余情况则为先手赢。

此题要注意两个问题,一:要填的空格,二:起始两边的和的大小;

若只考虑后手赢的情况,有两个阶段。一,两边都有空格时:先手为了让自己赢,在和大的一边放9,后手也只能在和小的这一边放9,则两边,和差sum,空格差k都不变;

二:只有一边有空格且另一边的和较大:因为不知先手放啥,特殊放0或9,故每一回合(k/2)保持放九个,故只有(k/2)*9==sum时,后手才会赢。

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 00to 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

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.

ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=2e5+10;
char s[M];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int x=0,y=0,z=0,a=0,b=0,c=0;
        scanf("%s",s+1);
        for(int i=1; i<=n/2; i++)
            if(s[i]=='?')
                x++;
            else
                a+=s[i]-'0';
        for(int i=n/2+1; i<=n; i++)
            if(s[i]=='?')
                y++;
            else
                b+=s[i]-'0';
        c=b-a;
        z=x-y;
        if(z%2)
            printf("Monocarp\n");
        else
        {
            z>>=1;//因为不知先手放啥,特殊放0或1,故每次保持放九个,类似巴什博弈
            if(z*9==c)
                printf("Bicarp\n");
            else
                printf("Monocarp\n");
        }
    }
    return 0;
}