Division game is a 2-player game.In this game, there is a matrix of positive integers with N rows and M columns.Players make their moves in turns. In each step, the current player selects arow. If the row contains all 1s, the player looses. Otherwise, the
player canselect any number of integers (but at least 1 and each of them should begreater than 1) from that row and then divides each of the selected integerswith any divisor other than 1. Forexample, 6 can be divided by 2, 3 and 6, but cannot be divided
by 1, 4 and 5.The player who first makes the matrix all 1s wins. In other words, if inhis/her move, player gets the matrix with all 1s, then he/she looses. Given thematrix, your task is to determine whether the first player wins or not. Assume thatboth of
the players will play perfectly to win.
Input
Thefirst line has a positive integer T, T <= 50, denoting the number oftest cases. This is followed by each test case per line.
Each test case starts with aline containing 2 integers N and M representing the number of rows and columnsrespectively. Both N and M are between 1 and 50 inclusive. Each of the next Nline each contains M integers. All these integers are between 2 and 10000inclusive.
Output
For each test case, the output contains a line in theformat Case #x: M, where x is the case number (starting from 1) and M is “YES”when the first player has a winning strategy and “NO” otherwise.
SampleInput Output for Sample Input
5
2 2
2 3
2 3
2 2
4 9
8 5
3 3
2 3 5
3 9 2
8 8 3
3 3
3 4 5
4 5 6
5 6 7
2 3
4 5 6
7 8 9
题意: 有一个n*m的矩阵,每个元素均在2-10000,两个游戏者轮流操作,每次可以选一行中的1个或者多个大于1的整数,把他们中每个数变成它的某个真因子,不能操作的输,如果在谁操作之前,矩阵中所有的数都是1,则他输
思路:把数变成真因子等价于拿掉它的一个或者多个素因子,这样,每行对应一个火柴堆,每个数的每个素因子看成一根火柴,就转换成Nim游戏了
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 10010; int vis[maxn], prime[maxn]; int n, m; void init() { memset(vis, 0, sizeof(vis)); prime[0] = 0; for (int i = 2; i < n; i++) if (!vis[i]) { prime[++prime[0]] = i; for (int j = i*i; j < maxn; j += i) vis[j] = 1; } prime[0] = 0; for (int i = 2; i < maxn; i++) if (!vis[i]) for (int j = i; j * i < maxn; j++) vis[j*i] = 1; for (int i = 2; i < maxn; i++) if (!vis[i]) prime[++prime[0]] = i; } int cal(int a) { int cnt = 1, sum = 0; while (a > 1 && cnt <= prime[0]) { while (a % prime[cnt] == 0) { a /= prime[cnt]; sum++; } cnt++; } return sum; } int main() { init(); int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); int ans = 0; for (int i = 0; i < n; i++) { int tmp = 0, a; for (int j = 0; j < m; j++) { scanf("%d", &a); tmp += cal(a); } ans ^= tmp; } printf("Case #%d: ", cas++); if (ans) printf("YES\n"); else printf("NO\n"); } return 0; }
UVA - 11859 Division Game