hdu 1536/ hdu 1944 S-Nim(sg函数)
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7751 Accepted Submission(s): 3266
The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.
The players take turns chosing a heap and removing a positive number of beads from it.
The first player not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:
Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).
If the xor-sum is 0, too bad, you will lose.
Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
The player that takes the last bead wins.
After the winning player's last move the xor-sum will be 0.
The xor-sum will change after every move.
Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.
Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?
your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. This means, as expected, that a position with no legal moves is a losing position.
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0
WWL
题意:首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去这个集合中的元素的个数
之后输入 一个m 表示接下来对于这个集合要进行m次询问
之后m行 每行输入一个n 表示有n个堆 每堆有n1个石子 问这一行所表示的状态是赢还是输 如果赢输入W否则L
#include <bits/stdc++.h>
using namespace std; const int MAXN = + ;
const int MAXM = + ; int f[MAXN];//f[0]存合法移动个数
int sg[MAXM];
bool exist[MAXN];//hash, sg不会超过合法移动个数MAXN void getSg(int n)
{
int i, j;
sg[] = ;
for (i = ; i <= n; ++i) {
memset(exist, false, sizeof(exist));
for (j = ; j <= f[] && f[j] <= i; ++j) {
exist[sg[i - f[j]]] = true;
}
for (j = ; j < MAXN; ++j) {
if (!exist[j]) {
sg[i] = j;
break;
}
}
}
} int main()
{
int k;//, s;
int m;
int l, hi;
int i, j;
int sum; while (~scanf("%d", &k)) {
if (k == ) {
break;
}
f[] = k;
for (i = ; i <= k; ++i) {
scanf("%d", &f[i]);
}
sort(f + , f + + k);
getSg(); scanf("%d", &m);
for (i = ; i < m; ++i) {
scanf("%d", &l);
sum = ;
for (j = ; j < l; ++j) {
scanf("%d", &hi);
sum ^= sg[hi];
}
if (sum != ) {
printf("W");
} else {
printf("L");
}
}
printf("\n"); }
return ;
}
#include <bits/stdc++.h>
using namespace std; const int MAXN = + ;
const int MAXM = + ; int s[MAXN];
int sg[MAXM];
int n;//s中的个数 int dfsSg(int x)
{
if (sg[x] != -) {
return sg[x];
}
int i;
bool vis[MAXN];//sg范围
memset(vis, false, sizeof(vis));
for (i = ; i < n && s[i] <= x; ++i) {
dfsSg(x - s[i]);
vis[sg[x - s[i]]] = true;
}
for (i = ; i <= x; ++i) {
if (!vis[i]) {
sg[x] = i;
break;
}
}
return sg[x];
} int main()
{
int k;//, s;
int m;
int l, hi;
int i, j;
int sum; while (~scanf("%d", &k)) {
if (k == ) {
break;
}
n = k;
for (i = ; i < k; ++i) {
scanf("%d", &s[i]);
}
sort(s, s + k);
memset(sg, -, sizeof(sg));
scanf("%d", &m);
for (i = ; i < m; ++i) {
scanf("%d", &l);
sum = ;
for (j = ; j < l; ++j) {
scanf("%d", &hi);
sum ^= dfsSg(hi);
}
if (sum != ) {
printf("W");
} else {
printf("L");
}
}
printf("\n"); }
return ;
}
hdu 1536/ hdu 1944 S-Nim(sg函数)的更多相关文章
- hdu 3032 Nim or not Nim? sg函数 难度:0
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 1848 Fibonacci again and again(SG函数)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)
提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...
- hdu 3980 Paint Chain 组合游戏 SG函数
题目链接 题意 有一个\(n\)个珠子的环,两人轮流给环上的珠子涂色.规定每次涂色必须涂连续的\(m\)颗珠子,无法继续操作的人输.问先手能否赢. 思路 参考 转化 第一个人取完之后就变成了一条链,现 ...
- HDU 1848 Fibonacci again and again SG函数做博弈
传送门 题意: 有三堆石子,双方轮流从某堆石子中去f个石子,直到不能取,问先手是否必胜,其中f为斐波那契数. 思路: 利用SG函数求解即可. /* * @Author: chenkexing * @D ...
- 多校6 1003 HDU5795 A Simple Nim (sg函数)
思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 3032 Nim or not Nim (sg函数)
加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...
- HDU 1729 Stone Game 石头游戏 (Nim, sg函数)
题意: 有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2 ...
随机推荐
- 007-搭建框架-开发AOP框架
一.代码地址 https://github.com/bjlhx15/smart-framework.git 二.代码编写 2.1.定义切面注解 增加Aspect注解 package com.lhx.s ...
- Oracle学习笔记—Db_name、Db_domain、Global_name、Service_name、Instance_name和Oracle_SID(转载)
转载自: Oracle中DB_NAME,SID,DB_DOMAIN,SERVICE_NAME等之间的区别 Db_name:对一个数据库(Oracle database)的唯一标识.这种表示对于单个数据 ...
- Spring学习笔记3—声明式事务
1 理解事务 事务:在软件开发领域,全有或全无的操作被称为事务.事务允许我们将几个操作组合成一个要么全部发生要么全部不发生的工作单元. 事务的特性: 原子性:事务是由一个或多个活动所组成的一个工作单元 ...
- ABAP rfc 发布webservice 错误
一.SICF 测试服务报错: 哎呀,找不到网页! 网站在检索此网址时出现错误.托管此网站的服务器可能关闭进行维护或配置不正确. HTTP ERROR 500
- python基础23 -----进程和线程
一.进程 1.什么是进程? 1.1 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 1.2 程序是指进程需要完成那些功能以及如何完成. 1.3 数据集 ...
- npm-folders
npm-folders Executable(可执行程序) 在全局模式下,可执行程序被链接到Unix的{prefix}/bin目录下,或者是Windows的{prefix}目录下. 在本地模式下,可执 ...
- 曾经遇到的坑------href="#"和href="javascript:void(0);"、href="javascript:;"
这个是为了 在点击此链接后回到页首,如果你写href="#"那么点击后会回到页首,这样影响操作. <a href="javascript:void 0" ...
- Web安全相关资料
Asp.net安全架构: http://www.cnblogs.com/luminji/category/381486.html
- Django 之models进阶操作
到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 ...
- CentOS7,将文本模式改成图形界面模式
在以前通过vi /etc/inittab,将3修改成5.但是在centOS7之后将修改的办法换掉了,执行systemctl set-default graphical.target.根据提示进行一步一 ...