2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 3 J. Addition and Multiplication (Game)

J. Addition and multiplication

Title link: https://ac.nowcoder.com/acm/contest/9983/J

Title description:

One day Niuniu and Niumei were playing a game. The rules are as follows:
There are n cards on the table, and each card is written with a positive integer. Niu Niu takes turns to perform the following operations:
1. If there is only one card left on the table, the game At the end, if the number on this card is odd, Niu Niu wins, otherwise Niu Mei wins.
2. The player in the current action selects two cards and sets the above numbers as X and Y respectively. Next, the player selects one of addition and multiplication and applies it to these two numbers. The result is Z, and the next two will be selected. Throw away a card, take a new card and put it on the table, write Z on it.
Assuming that both parties act with the best strategy, who will win in the end?

Enter a description:

A positive integer n in the first line represents the starting card number.
The positive integer ai separated by n spaces in the second line represents the number on the starting card.
1 ≤ n≤ 10^6
1 ≤ ai ​≤10^9

Output description:

If Niu Niu can win, output NiuNiu, otherwise output NiuMei.

Example 1:

Input
3
233 2333 23333
Output
NiuMei

Example 2:

Input
4
1 1 1 1
Output
NiuNiu

Problem-solving ideas:

When the game problem
n = 1 , if it is an odd number, the bull will win, and if it is an even number, the bull will win.
When n> 1 , if the last operation is Niu Mei (backhand), Niu Mei wins. Because in any case, two even numbers must be generated. (If both are odd numbers, then odd number + odd number = even number, if there are even numbers, do multiplication or get even numbers).
If there is at most one even number in the initial stage, the operations of Niu Niu (first hand) can all become odd numbers, and Niu Mei (second hand) can produce at most one even number, so there is only one even number at most, and Niu Niu (first hand) wins.
If there are more than two even numbers at the beginning, there must be at least two even numbers when Niumei (backhand) operates, so eventually Niumei (backhand) wins.

code show as below:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
    
    
	int n,cnt=0;
	cin>>n;
	int a[1000010]={
    
    0};
	for(int i=1;i<=n;i++){
    
    
		cin>>a[i];
		if(a[i]%2==0)
			cnt++;
	}
	if(n==1){
    
    
		if(a[1]&1)
			cout<<"NiuNiu"<<endl;
		else
			cout<<"NiuMei"<<endl;
		return 0;
	}
	if((n&1)||cnt>=2){
    
    
		cout<<"NiuMei"<<endl;
		return 0;
	}
	cout<<"NiuNiu"<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/113703249