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 ...
随机推荐
- 前端框架之jQuery(二)----轮播图,放大镜
事件 页面载入 ready(fn) //当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. $(document).ready(function(){}) -----------> ...
- 设计一个算法,採用BFS方式输出图G中从顶点u到v的最短路径(不带权的无向连通图G採用邻接表存储)
思想:图G是不带权的无向连通图.一条边的长度计为1,因此,求带顶点u和顶点v的最短的路径即求顶点u和顶点v的边数最少的顶点序列.利用广度优先遍历算法,从u出发进行广度遍历,类似于从顶点u出发一层一层地 ...
- hive批量执行sql命令及使用小技巧
root@hadoop-senior hive-0.13.1]$ bin/hive -helpusage: hive -d, --define <key=value> Variable s ...
- C++程序设计练习(一)
// 1. 在屏幕上输出内容 #include<iostream> using namespace std; int main(){ int i= 1; cout<<" ...
- Percona备份mysql全库及指定数据库(完整备份与增量备份)
Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份) Xtrabackup简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对I ...
- (转)fiddler使用简介--其一
原文地址:https://www.cnblogs.com/miantest/p/7289694.html Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行 ...
- More on Class Loading and Initialization
上一篇博客中对于类的加载和初始化进行了详细的说明,但上一篇博客代码中的main()所在的类为导出类, 对其中一些问题的理解可能会引起误导和不明确,所以补充这篇博客进一步说明.以下面的代码为例进行说明: ...
- MySQL数据库(1)_MySQL数据库介绍与安装
一.数据库相关概念的简介 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展 ...
- Python进阶(4)_进程与线程 (python并发编程之多进程)
一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...
- Android 工具类 SharedPreferences 封装
SharedPreferences 是 Android 数据存储方式中的一种,特别适合用来存储少量的.格式简单的数据,比如应用程序的各种配置信息,如是否打开音效,是否开启震动等等. SharedPre ...