题目
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为整数且小于2e5)位组成,其中有整数个数位被污染,现在A和B可以轮流给被污染数位赋值(0-9),Monocarp先来,若最后序列前后两端数位和不等,Monocarp赢,否则Bicarp赢,两方都选最优策略。
问题分析
先分别获取两部分非擦除数的和sum1、sum2,以及两半数中’?‘的个数num1、num2。并记差值为x,那么问题统一为:前一半数的和为x+num1个’?’(值可不一,下同),后一半数的和为num2个’?’。
①sum1==sum2:
当num1不等于num2时,先手一定赢。反之则后者赢。
②假如sum1>sum2
(1)num1>=num2时,先手可以在一边一直放9,后手弥补不了差距。先手必胜。
(2)num1<num2时,前2n次同上,剩下的次数中,先手放sum1时,后手只能是放sum2来使得sum1+sum2=9;所以当9|sum1-sum2时后手胜。不然的话后手是必输的。
#include<bits/stdc++.h>
#define ll long long
const ll mod=1e9+7;
const int maxn=2e5+100;
using namespace std;
int n,m,k,t;
char c;
int num1,num2;
int sum1,sum2;
int main(){
cin>>n;
for(int i=1;i<=n/2;i++){
cin>>c;
if(c=='?')num1++;
else sum1+=(c-'0');
}
for(int i=n/2+1;i<=n;i++){
cin>>c;
if(c=='?')num2++;
else sum2+=(c-'0');
}
sum1+=num1/2*9;
sum2+=num2/2*9;
if(sum1==sum2){printf("Bicarp\n");}
else{printf("Monocarp\n");}
}