您的位置:首页 > 其它

SG函数题目

2013-11-29 20:10 246 查看
HDU Fibonacci again and again

思路:

把整个游戏看成三个子游戏,然后求游戏的和

关键理解g(x) = mex(g(y), y€f(x)) , f(x)表示由x点可达的点,

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();
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: