UVA - 11859 Division Game

 

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值