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\n",t,ans == 0 ? "NO" : "YES");
29     }
30     return 0;
31 }
View Code

 

转载于:https://www.cnblogs.com/cyb123456/p/5813498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用AngularJs编写的简单 益智游戏(附源代码)  这是一个简单的 javascript 项目。这是一个拼图游戏,也包含一个填字游戏。这个游戏玩起来很棒。有两个不同的版本可以玩这个游戏。你也可以玩填字游戏。 关于游戏 这款游戏的玩法很简单。如上所述,它包含拼图和填字游戏。您可以通过移动图像来玩滑动拼图。您还可以选择要在滑动面板中拥有的列数和网格数。 另一个是填字游戏。在这里你只需要找到浏览器左侧提到的那些单词。 要运行此游戏,您需要在系统上安装浏览器。下载并在代码编辑器中打开此项目。然后有一个 index.html 文件可供您修改。在命令提示符中运行该文件,或者您可以直接运行索引文件。使用 Google Chrome 或 FireFox 可获得更好的用户体验。此外,这是一款多人游戏,双方玩家都是人类。 这个游戏包含很多 JavaScript 验证。这个游戏很有趣,如果你能用一点 CSS 修改它,那就更好了。 总的来说,这个项目使用了很多 javascript 和 javascript 库。如果你可以添加一些具有不同颜色选项的级别,那么你一定可以利用其库来提高你的 javascript 技能。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值