Logical error

After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.

An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.

In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.


The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).

Assume that both players play optimally. Who will win the game?

Input
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).

Output
Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game.

Examples
input
2 2
output
Malvika
input
2 3
output
Malvika
input
3 3
output
Akshat

So my code is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <bits/stdc++.h>


#define fl(n) for(int i = 0; i < n; i++)


#define ll long long
#define nl endl
#define init -9999
#define INF 1e9
#define u unsigned

using namespace std;


int main()
{
    /*

    Ideal 0 moves game:
    0 0 0
    0 0 0
    Turn 1 move -auto-
    1 1 1
    1 0 0
    Turn 2 move -auto-
    1 1 1
    1 1 1
    Turn 1 has no moves
    Turn 2 player wins.
    */

    int n,m;
    cin >> n >> m;

    int sticks = n+m;
    int rem = sticks;
    int win;
    int turn = 1;

    if(n == 1) {cout << "Akshat"; return 0;}
    if(sticks > n*m) {cout << "Akshat"; return 0;}


    for(int i = 0; i < sticks; i++)
    {
        if(turn == 1)
        {
            rem -= 2;
            //cout << rem << endl;
            if(rem <= 1) { win = 1; break; }
            turn = 2;
        }
        else if(turn == 2)
        {
            rem -= 2;
            //cout << rem << endl;
            if(rem <= 1) { win = 2; break; }
            turn = 1;
        }
    }
    if(win == 1) cout << "Akshat";
    else if(win == 2) cout << "Malvika";

    return 0;
}


And it works for alot of test cases but idk why it doesn't output corrrct answers for the rest , meanwhile this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
 
using namespace std;
 
int main(){
    int n, m, moves;
    cin >>n>>m;
    moves = min(n,m);
    if(moves%2==0)
        cout << "Malvika" << endl;
    else
        cout << "Akshat" << endl;
    return 0;
}


Seems to work extremely well and idk the maths behind it .. can someone explain?
Last edited on
Although the rules do not state it explicitly, it appears that when a stick is removed, all of the other intersection points related to the stick go with it. So, if you have 3 horizontal sticks (H1, H2, H3) and 3 vertical sticks (V1, V2, V3), the 9 intersection points are (H1, V1), (H1, V2), (H1, V3), (H2, V1), (H2, V2), (H2,V3), (H3, V1), (H3, V2), and (H3,V3). Say you take away the sticks at (H2, V2)--so you take away H2 and V2. that means that in addition to the selected intersection, the following intersections also disappear: (H1, V2), (H2, V1), (H2, V3), and (H3, V2). So, the remaining intersections are only those related to the remaining sticks.

When all of the horizontal sticks OR all of the vertical sticks have been removed, there will be no more intersections, and the result will be known. Since each round both a horizontal and a vertical stick are removed, the number of rounds to determine the result is the smaller of the number of vertical and horizontal sticks.

Since there are 2 players, the players alternate turns. Akshat takes the first turn, Malvika takes the second turn, Akshat the third, Malvika the fourth and so on. So, every odd number turn is taken by Akshat and every even number turn is taken by Malvika. So, if there are an odd number of possible turns, Akshat takes the last turn and wins. Likewise, if there are an even number of possible turns, Malvika takes the last turn and wins.

So, line 8 determines how many moves are required to determine the winner. Then, line 9 uses the modulo operator to determine if this number is divisible by 2 (result == 0, even) or not (result == 1, odd). Based on whether number of moves is even or not, the winner is declared. That is why the second code snippet works.

Note: the homework assignment is ambiguous. You could interpret the assignment as saying the "intersection" remains on the board until both related sticks are removed. At that time, you need to deal with game theory in order to "assume that both players play optimally". However, for a beginner's homework assignment, I think the first assumption is correct.

Although the rules do not state it explicitly, it appears that when a stick is removed, all of the other intersection points related to the stick go with it. So, if you have 3 horizontal sticks (H1, H2, H3) and 3 vertical sticks (V1, V2, V3), the 9 intersection points are (H1, V1), (H1, V2), (H1, V3), (H2, V1), (H2, V2), (H2,V3), (H3, V1), (H3, V2), and (H3,V3). Say you take away the sticks at (H2, V2)--so you take away H2 and V2. that means that in addition to the selected intersection, the following intersections also disappear: (H1, V2), (H2, V1), (H2, V3), and (H3, V2). So, the remaining intersections are only those related to the remaining sticks.

When all of the horizontal sticks OR all of the vertical sticks have been removed, there will be no more intersections, and the result will be known. Since each round both a horizontal and a vertical stick are removed, the number of rounds to determine the result is the smaller of the number of vertical and horizontal sticks.

Since there are 2 players, the players alternate turns. Akshat takes the first turn, Malvika takes the second turn, Akshat the third, Malvika the fourth and so on. So, every odd number turn is taken by Akshat and every even number turn is taken by Malvika. So, if there are an odd number of possible turns, Akshat takes the last turn and wins. Likewise, if there are an even number of possible turns, Malvika takes the last turn and wins.

So, line 8 determines how many moves are required to determine the winner. Then, line 9 uses the modulo operator to determine if this number is divisible by 2 (result == 0, even) or not (result == 1, odd). Based on whether number of moves is even or not, the winner is declared. That is why the second code snippet works.


Question: Why is the min function used for the N & M why can't I just use n % 0 or m % 0 instead?

You said
So, line 8 determines how many moves are required to determine the winner.

But I didn't quite get what you meant by how many moves are required to determine the winner, How does the min function get that?
Last edited on
Question: Why is the min function used for the N & M why can't I just use n % 0 or m % 0 instead?

First of all, n % 0 is meaningless. I assume you meant n % 2 and m % 2.

But the answer to your question is in my second paragraph above:

When all of the horizontal sticks OR all of the vertical sticks have been removed, there will be no more intersections, and the result will be known. Since each round both a horizontal and a vertical stick are removed, the number of rounds to determine the result is the smaller of the number of vertical and horizontal sticks.


I don't really know how to better explain it than that. What that means is that you ARE using n%2 or m%2, but you don't know which one until you do the min operation.

If this does not make sense to you, explain how you would like to "just use n % 0 or m % 0 instead?" so I understand what you are asking.
Each move removes one row and one column, and the game ends when you run out of rows or columns.

I don't really know how to better explain it than that. What that means is that you ARE using n%2 or m%2, but you don't know which one until you do the min operation.

If this does not make sense to you, explain how you would like to "just use n % 0 or m % 0 instead?" so I understand what you are asking.


I meant %2 yes haha but you mean we get the min between n and m due to what? i didnt understand that well sorry :((
Topic archived. No new replies allowed.