题意:

一个取火柴游戏,可以取的数在一个集合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的更多相关文章

  1. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  2. (转) Playing FPS games with deep reinforcement learning

    Playing FPS games with deep reinforcement learning 博文转自:https://blog.acolyer.org/2016/11/23/playing- ...

  3. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  4. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  5. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  6. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  7. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  8. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  9. [转]JavaScriptCore by Example

    原文:http://www.bignerdranch.com/blog/javascriptcore-example/ JavaScriptCore is not a new framework; i ...

随机推荐

  1. Nginx缓存配置及nginx ngx_cache_purge模块的使用

    ngx_cache_purge模块的作用:用于清除指定url的缓存 下载地址:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz   1. ...

  2. Android 国内镜像

    Android SDK官网国内很难直接访问,除了FQ/VPN等方法还是很不方便. 原有的Android SDK直接下载因http://dl-ssl.google.com/android/reposit ...

  3. POJ3186:Treats for the Cows(区间DP)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  4. 三分钟读懂Oracle数据库容灾架之DataGuard

    Oracle数据库目前依然处于商用数据库的霸主地位. 运行在Oracle数据库上的核心业务及核心数据的安全性尤为重要. 目前市场上针对Oracle数据库常见的容灾产品大致可以分为两大类. Oracle ...

  5. [转载]通过jQuery的attr修改onclick

    var js = "alert('B:' + this.id); return false;"; // creates a function from the "js&q ...

  6. Spring DI模式 小样例

           今儿跟同事讨论起来spring早期的,通过大篇幅xml的配置演变到今天annotation的过程,然后随手写了个小样例,感觉还不错,贴到这里留个纪念. 样例就是用JAVA API的方式, ...

  7. [RxJS] Transformation operator: map and mapTo

    We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...

  8. innodb结构解析工具---innodb_ruby

    1.下载ruby并安装ruby: ftp://ftp.ruby-lang.org/pub/ruby/ ftp://ftp.ruby-lang.org/pub/ruby/ruby-2.3-stable. ...

  9. [转] How to dispatch a Redux action with a timeout?

    How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...

  10. CentOS 6.4 编译 Hadoop 2.5.1

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4058956.html ...