This is a game theory problem with a dynamic programming solution. We will write what's commonly known as a "minimax" recursive function that evaluates the result of the game when both players play optimally. We are given inputs $1 \leq n1, n2, k1, k2 \leq 50$. We say that $\t{dp[n1][n2][fl]}$ is the score of the first player ($0$ or $1$, where $1$ means first player wins) for $n1$, $n2$ balls for first and second players, respectively, and $\t{fl=1}$ for first player's turn, $\t{fl=0}$ for second player's turn.
Our function $\t{minimax(n1, n2, fl)}$ returns the score of the game, if both players play optimally, where $n1$ is balls of first player, $n2$ is balls of second player, and $fl$ is $1$ for first player's turn, $fl$ is $0$ for second player's turn.