SGU 153.Playing with matches
题意:
一个取火柴游戏,可以取的数在一个集合S内,S必包含1,且不超过9个数,每个数都不大于9.最后取完者失败。
求n(n<10^9)根火柴时先取的胜利还是后取的胜利.
Solution:
典型的求SG函数的问题,由于n很大,而S集合的数比较少而且不超过10,所以可以通过找到它的循环节。
估算可知循环节不会超过512。一开始试图通过求S集合的公约数找到循环长度,但发现这是错误的。
下面的程序参考了别人的程序,暴力找循环节。
#include <stdio.h>
#include <string.h>
int a[12],n,m;
int SG[1060],hash[1060];
void GetSG(int Array[], int N)
{
int i, j;
memset(SG, 0, sizeof(SG));
for(i = 0; i <= N; i++)
{
memset(hash, 0, sizeof(hash));
for(j = 0; j<=m; j++)
{
if(i-Array[j]>0)
hash[SG[i - Array[j]]] = 1;
} for(j = 0; j <= N; j++)
{
if(hash[j] == 0)
{
SG[i] = j;
break;
}
}
}
} int main()
{
int T;
a[0]=1;
scanf("%d",&T);
while(T--)
{
memset(SG,0,sizeof(SG));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
GetSG(a,1050);
int k=0;
for(int l=512;l>=1;l--)
{
if(memcmp(SG+10,SG+10+l,l*sizeof(int))==0)
{
k=l;break;
}
}
if(n<9)
{
if(SG[n]>0)printf("FIRST PLAYER MUST WIN\n");
else printf("SECOND PLAYER MUST WIN\n");
}
else
{
if(SG[9+(n-9)%k]>0)printf("FIRST PLAYER MUST WIN\n");
else printf("SECOND PLAYER MUST WIN\n");
}
}
return 0;
}
SGU 153.Playing with matches的更多相关文章
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- (转) Playing FPS games with deep reinforcement learning
Playing FPS games with deep reinforcement learning 博文转自:https://blog.acolyer.org/2016/11/23/playing- ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- poj 动态规划的主题列表和总结
此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...
- [转]JavaScriptCore by Example
原文:http://www.bignerdranch.com/blog/javascriptcore-example/ JavaScriptCore is not a new framework; i ...
随机推荐
- 转---在ASP.NET MVC中实现登录后回到原先的界面
有这样的一个需求:提交表单,如果用户没有登录,就跳转到登录页,登录后,跳转到原先表单提交这个页面,而且需要保持提交表单界面的数据. 提交表单的页面是一个强类型视图页,如果不考虑需要保持提交表单界面的数 ...
- C++ string 构造的陷阱
先看代码 #include<iostream> #include<string> using namespace std; int main(int argc, char ** ...
- InfoSphere BigInsights 安装部署
InfoSphere BigInsights 有三个版本:基础版.企业体验版.企业版.基础版是免费的,但是少了一些功能:企业体验版是在购买企业版之前又来体验测试的:如果要部署企业版,应该购买企业版.安 ...
- html或jsp实现打印三种方法
1.使用window.print()方法 优点:支持多浏览器 缺点:取消打印,隐藏打印不必要的信息后再显示比较麻烦 如下实现,可以打印当前页面 <input name ="Button ...
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率.下面的例子中实现了用BufferedInputStream与FileInputStream实 ...
- Docker的基本操作
容器基本操作 1.启动容器 $docker run image [COMMAND] [ARG…] run在新容器中执行命令 2.启动交互式容器 $docker run -i -t IMAGE /bin ...
- 第一贱-UILabel
UILabel *label = [[UILabel alloc]init]; label.frame = CGRectMake(100, 100, 100, 100); label.text = @ ...
- Oracle、MySql、Sql Server比对
1. 价格 MySql:廉价(部分免费):当前,MySQL採用双重授权(DualLicensed),他们是GPL和MySQLAB制定的商业许可协议.假设你在一个遵循GPL的自由(开源)项目中使用 ...
- 遍历datatable的几种方法【转载】
遍历datatable的方法2009-- :02方法一: DataTable dt = dataSet.Tables[]; ; i < dt.Rows.Count ; i++) { string ...
- Spring Data MongoDB example with Spring MVC 3.2
Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...