SG函数题目
思路:
把整个游戏看成三个子游戏,然后求游戏的和
关键理解g(x) = mex(g(y), y€f(x)) , f(x)表示由x点可达的点,
import java.util.Arrays;
import java.util.Scanner; public class Main {
public int[] fib = new int[20];
public boolean[] vt = new boolean[20];
public static int[] g = new int[1002]; public void init() {
fib[1] = 1; fib[2] = 2;
for (int i = 3; i < 20; ++i) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
public void dfsSG() {
//首先找出确定的点一般为0点
g[0] = 0; g[1] = 1;
for (int i = 2; i <= 1000; ++i) {
Arrays.fill(vt, false);
//然后枚举g(y)标记, g(x) = mex(g(y), y 属于 F(x));
for (int j = 1; fib[j] <= i; ++j) {
vt[g[i - fib[j]]] = true;
}
//mex()过程
for (int j = 0; j <= 1000; ++j) {
if (!vt[j]) {
g[i] = j;
break;
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Main main = new Main();
main.init(); main.dfsSG();
int m, n, p;
while (in.hasNext()) {
m = in.nextInt();
n = in.nextInt();
p = in.nextInt();
if (m == 0 && n == 0 && p == 0) break;
if ((g[m] ^ g[n] ^ g[p]) != 0) {
System.out.println("Fibo");
} else {
System.out.println("Nacci");
}
}
}
}
思路:同上。。只是每一步的策略不同罢了。。
import java.util.Arrays;
import java.util.Scanner; public class Main {
public static Scanner in = new Scanner(System.in);
public static int[] s;
public static int[] g = new int[10001];
/*
* 注意vt的取值,我们只要的最大之只要是s的大小 + 1即可。
* 因为我们最后往后走k不,如果这k个得到的是0-k那么我们的取值就是k+1
* 这是最大的了
*/
public static boolean[] vt = new boolean[107]; public static int k;
public static void dfsSG() {
g[0] = 0;
for (int i = 1; i <= 10000; ++i) {
Arrays.fill(vt, false);
for (int j = 0; j < k; ++j) {
if (i - s[j] < 0) break;
vt[g[i - s[j]]] = true;
}
for (int j = 0; j <= 107; ++j) {
if (!vt[j]) {
g[i] = j;
break;
}
}
}
}
public static void main(String[] args) { while (in.hasNext()) {
k = in.nextInt();
if (k == 0) break;
s = new int[k];
for (int i = 0; i < k; ++i) s[i] = in.nextInt();
Arrays.sort(s);
dfsSG();
int m = in.nextInt();
while ((m--) != 0) {
int sz = in.nextInt();
int ans = 0;
for (int i = 0; i < sz; ++i) {
int idx = in.nextInt();
ans ^= g[idx];
}
if (ans == 0) {
System.out.print("L");
} else {
System.out.print("W");
}
}
System.out.println();
}
}
}
SG函数题目的更多相关文章
- BZOJ1188 [HNOI2007]分裂游戏(SG函数)
传送门 拿到这道题就知道是典型的博弈论,但是却不知道怎么设计它的SG函数.看了解析一类组合游戏这篇论文之后才知道这道题应该怎么做. 这道题需要奇特的模型转换.即把每一个石子当做一堆石子,且原来在第i堆 ...
- sg函数与博弈论
这个标题是不是看起来很厉害呢... 我们首先来看一个最简单的游戏.比如我现在有一堆石子,有p个,每次可以取走若干个(不能不取),不能取的人就输了. 现在假设有两个人要玩这个游戏,一个人先手,一个人后手 ...
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- HDU1848 Fibonacci again and again SG函数
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- SG函数 专题练习
[hdu1536][poj2960]S-Nim 题意 题意就是给出一个数组h,为每次可以取石子的数目. 然后给你n堆石子每堆si.求解先手能不能赢? 分析 根据\(h\)数组预处理出\(sg[i]\) ...
- HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)
Fibonacci again and again Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu-------(1848)Fibonacci again and again(sg函数版的尼姆博弈)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- SG函数
入门一: 首先来玩个游戏,引用杭电课件上的: (1) 玩家:2人:(2) 道具:23张扑克牌:(3) 规则:游戏双方轮流取牌:每人每次仅限于取1张.2张或3张牌:扑克牌取光,则游戏结束:最后取牌的一方 ...
- hdoj 1729 Stone Games(SG函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729 看了题目感觉像Nim,但是有范围限制,有点不知道SG函数该怎么写 看了题解,最后才明白该怎么去理 ...
随机推荐
- Backup: Date and Time in Perl6
时间 Date #Operators ==, <, <= , >, >=, !=, eq, lt, le # Methods $date = Date.new(YEAR, MO ...
- 【原创】纯干货,Spring-data-jpa详解,全方位介绍。
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- Length of Last Word
class Solution { public: int lengthOfLastWord(string s) { ; ; while(s[i]&&s[i]==' ') i++; // ...
- SPOOL、SQLLOADER数据导出导入的一点小总结
1.SQLLOADER的CONTROL文件 //**************************************************************************** ...
- jquery.rotate.js库中的rotate函数怎么用。
rotate是jQuery旋转rotate插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .Opera 9 .Google Chrome,高级浏览 ...
- PHP serialize & JSON 解析
对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Program ...
- java用freemarker导出数据到word(含多图片)
一.制作word模版 新建word文档,按照需要设置好字体等各种格式:这里为了显得整齐使用了无边框的表格. 将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了) 然后用 ...
- AtomicInteger类保证线程安全的用法
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...
- MAVEN安装过程
maven 的压缩包地址: http://pan.baidu.com/s/1kT4ckGf 第三方资源jar包地址: http://pan.baidu.com/s/1i3vtgED
- 线程和进程详解(以java为例具体说明)
详细参见http://ifeve.com/java-concurrency-thread-directory/ 一.线程概述 线程是程序运行的基本执行单元.当操作系统(不包括单线程的操作系统,如微软早 ...