UVa11859

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的整数把它们中的每个数都变成它的某个真因子,比如12可以变成1,2,3,4,5.不能操作的输,也就是说,谁在操作之前,矩阵中的所有数是1,则输。题目要求判断第一个人是否能获胜。

分析:

      题目要求让一个数变为它的真因子,等价于拿掉一个或者多个它的素因子。(12拿掉素因子2变为6,拿掉素因子3变为4,拿掉两个素因子2为3)这样,就可以每行看做一个火柴堆,每个数的素因子看成火柴。

根据 Bouton定理,状态(x1,x2,x3)为必败状态当且仅当x1 xor x2 xor x3 =0(xor为异或操作)所以我们只需要统计每一行的素因子个数,求和,异或一下判断是否为0即可。

怎么求素因子呢?

可以仿造用Eratosthenes快速构造素数表,

Eratosthenes构造素数表是筛选掉不超过N的每个数的整倍数。如筛选2的时候2*2,2*3,2*4.。。。

 1 #include <cstdio>
 2 #define MAX_N 10000
 3 int cnt[MAX_N + 1];
 4 // 统计各个正整数的因子的个数
 5 int cnt_prim_factornum(){
 6     for(int i = 2 ; i <= MAX_N ; i++)if(!cnt[i]){
 7         int t = i;
 8         while(t <= MAX_N){
 9             for(int j = t ; j <= MAX_N ; j += t) cnt[j]++;
10             t *= i; // 将素数i的所有幂次都用来筛
11         }
12     }
13 }
14 int main(){
15     cnt_prim_factornum();
16     int T; scanf("%d",&T);
17     for(int t = 1 ; t <= T ; t++){
18         int n,m; scanf("%d%d",&n,&m);
19         int ans = 0,sum;
20         for(int i = 0 ; i < n ; i++){
21             sum = 0;
22             for(int j = 0 ; j < m ; j++){
23                 int tmp; scanf("%d",&tmp);
24                 sum += cnt[tmp];
25             }
26             ans ^= sum;
27         }
28         printf("Case #%d: %s
",t,ans == 0 ? "NO" : "YES");
29     }
30     return 0;
31 }
View Code
原文地址:https://www.cnblogs.com/cyb123456/p/5813498.html