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 ...
随机推荐
- python——random模块
用法示例: import random # 1)随机小数 print(random.random()) # 获取大于0且小于1 之间的小数 random.random() print(random.u ...
- 我的第一个JSP
首先在新建一个Webproject 再在WebRoot以下new一个HelloWorld.jsp 改动body里面的内容 <body> <h1>HelloWorld:& ...
- Js全局异常捕获
重新window.onerror方法就行了 window.onerror = handleError function handleError(msg,url,l) { var txt="T ...
- cache与buffer的区别
Cache vs Buffer 高速缓存和缓冲区 缓存区cache和缓冲区buffer都是临时存储区,但它们在许多方面有所不同.缓冲区buffer主要存在于RAM中,作为CPU暂时存储数据的区域,例如 ...
- windows中使用Findwindow函数与FindWindowEx函数来实现自动控制、触发第三方软件事件的方法
FindWindow 用来根据类名和窗口名来得到窗口句柄的.但是这个函数不能查找子窗口,也不区分大小写. 如果要从一个窗口的子窗口中查找需要使用FindWindowEX. 如果要搜索的外部程序的窗口标 ...
- PyQt4设置窗口左上角的小图标
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...
- 剑指offer 面试51题
面试51题: 题目:数组中的逆序对 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007 ...
- Binary Search in Java
关于折半查找中的几个注意点. Version 1: public static <T extends Comparable<? super T>> int binSearch( ...
- HAProxy详解
HAProxy概述与配置 一.HAProxy概述 HAProxy是由 WillyTarreau开发的一款具备高可用性.负载均及基于 TCP和 HTTP的应用代理开源软件,基于HAProxy的负载均衡架 ...
- C语言中auto,register,static,const,volatile的区别
1)auto 这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是aut ...