HDU 1536 sg函数
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7262 Accepted Submission(s): 3074
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.
- /******************************
- code by drizzle
- blog: www.cnblogs.com/hsd-/
- ^ ^ ^ ^
- O O
- ******************************/
- #include<bits/stdc++.h>
- #include<map>
- #include<set>
- #include<cmath>
- #include<queue>
- #include<bitset>
- #include<math.h>
- #include<vector>
- #include<string>
- #include<stdio.h>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #pragma comment(linker, "/STACK:102400000,102400000")
- using namespace std;
- #define A first
- #define B second
- const int mod=;
- const int MOD1=;
- const int MOD2=;
- const double EPS=0.00000001;
- typedef __int64 ll;
- const ll MOD=;
- const int INF=;
- const ll MAX=1ll<<;
- const double eps=1e-;
- const double inf=~0u>>;
- const double pi=acos(-1.0);
- typedef double db;
- typedef unsigned int uint;
- typedef unsigned long long ull;
- int k;
- int sg[];
- int a[];
- int flag[];
- int q,m,exm;
- void init()
- {
- sg[]=;
- for(int i=;i<=;i++)
- {
- memset(flag,,sizeof(flag));
- for(int j=;j<=k;j++)
- {
- if(i-a[j]>=)
- {
- flag[sg[i-a[j]]]=;
- }
- }
- for(int j=;;j++)
- {
- if(flag[j]==){
- sg[i]=j;
- break;
- }
- }
- }
- }
- int main()
- {
- while(scanf("%d",&k)!=EOF)
- {
- if(k==)
- break;
- for(int i=;i<=k;i++)
- scanf("%d",&a[i]);
- init();
- scanf("%d",&q);
- for(int i=;i<=q;i++)
- {
- scanf("%d",&m);
- int ans=;
- for(int j=;j<=m;j++)
- {
- scanf("%d",&exm);
- ans^=sg[exm];
- }
- if(ans==)
- printf("L");
- else
- printf("W");
- }
- printf("\n");
- }
- return ;
- }
HDU 1536 sg函数的更多相关文章
- hdu 1536 SG函数模板题
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- hdu 2147 SG函数打表(手写也可以) 找规律
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others) Total ...
- hdu 1848(SG函数)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 1847(SG函数,巴什博弈)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1848 SG函数博弈
Fibonacci again and again Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1 ...
- hdu 2999 sg函数(简单博弈)
Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/ ...
- hdu 1536 sg (dfs实现)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- SG 函数初步 HDU 1536 && HDU 1944
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1944 pid=1536"> http://acm.hdu.edu.cn/showpr ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
随机推荐
- UITableView优化的方法
使用不透明视图. 不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值). 其中的特例包括背景色,它的alpha值应该为1(例如 ...
- Python学习笔记(0)
Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...
- Java使用BigDecimal精确计算的简单公式计算器
由于工作需要,写了一个使用BigDecimal运算的精确计算的计算器(然后发现其实比不用BigDecimal的并好不到哪里去) 只能做加减乘除 double类型的数字在千万级别的时候会转成科学计数法, ...
- 《BI那点儿事》数据流转换——透视
这个和T-SQL中的PIVOT和UNPIVOT的作用是一样的.数据透视转换可以将数据规范或使它在报表中更具可读性. 通过透视列值的输入数据,透视转换将规范的数据集转变成规范程度稍低.但更为简洁的版本. ...
- iOS - Xcode 常用快捷键
Xcode 常用快捷键 1)文件: command + shift + n 新建项目 command + n 新建文件 command + control + n 新建空文件 command + o ...
- 16 SQL Tuning Overview
16.1 Introduction to SQL Tuning Identifying high load or top SQL statements that are responsible for ...
- 【UFLDL】Exercise: Convolutional Neural Network
这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...
- Evolutionary Computing: 3. Genetic Algorithm(2)
承接上一章,接着写Genetic Algorithm. 本章主要写排列表达(permutation representations) 开始先引一个具体的例子来进行表述 Outline 问题描述 排列表 ...
- 基于讯为4412开发板的Android开发流程
讯为4412开发板 使用三星2410芯片,基于arm9架构,由于自己电脑硬件的局限,只能跑Android4.0.3系统. 1.Uboot这个直接使用官方镜像烧写就可以了,一般情况不用去重复烧写. 略 ...
- cookie预:
什么是cookie? cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie ...