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

UVA - 11859 Division Game

时间: 2024-08-25 21:28:27

UVA - 11859 Division Game的相关文章

UVA 11859 Division Game (Nim博弈)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32746 题意:有一个n*m(1<=n,m<=50)矩阵,每个元素均为2~10000之间的正整数,两个游戏者轮流操作.每次可以选一行中的1个或者大于1的整数,把他们中的每个数都变成它的某个真因子,比如12可以边长1,2,3,4或者6,不能操作的输. 分析:考虑每个数包含的素因子个数(比如12=2*2*3包含3个素因子),则让一个数"变成它的素因子"

UVA 11859 Division Game[Nim游戏]

题意:给定一个N*M的矩阵,每次可以选择同一行中的若干个数,把它们变成它们的质因子.问说先手的可否获胜. 同一行相当于1堆,数量就是所有数的质因子个数之和 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const int N=55; inline int read

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

uva 725 Division(除法)暴力法!

uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that t

UVA 725 Division ( 找出 abcde / fghij = N的形式—— 暴力枚举 )

Division Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divide

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =

暴力枚举 UVA 725 Division

题目传送门 1 /* 2 暴力:对于每一个数都判断,是否数字全都使用过一遍 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <map> 11 #include <set> 12

UVa 11859 (Nim) Division Game

把每一行m个数所有的素因子看做一堆,就把问题转化为n堆的Nim游戏. 然后预处理一下10000以内每个数素因数的个数,再根据书上的Bouton定理,计算一下n行素因数个数的异或和. 为0是先手必败局面,输出NO,否则输出YES 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxp = 10000; 5 int f[maxp + 10]; 6 7 int main() 8 { 9 //freopen("in.tx

UVa 725 - Division(枚举简单题)

今日无事,写篇日记.这是我写的第一道uva题(uva是算法竞赛入门经典里使用例题的题目来源),正值暑假,前几天看了书上中文题目和解析,玩了几日san11,又重拾起acm,今天晚上写了一下还是花了点时间. 题目:给你一个数字n,用0~9,10个数字组成两个五位数,使得他们的商为n,按顺序输出所有结果. 1.这里的除法不是c中的除(/)而是整除,一开始以为是整除,但那样79546 / 01283 = 62是一组解,79546+1 / 01283 = 62也是一组解了. 2.复杂度:枚举分子然后分子乘