hdu 1848:
题意:
给三堆石子,数量分别是 m,n,p,每次取只能取fib数个石子,求最后谁赢。
解析:
学习:
sg函数的应用。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int fib[100];
void fibTable()
{
fib[0] = fib[1] = 1;
for (int i = 2; i < 20; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
}
}
int sg[1010];
void sgTable()
{
sg[0] = 0;
for (int j = 1; j <= 1000; j++)
{
set<int> s;
for (int i = 0; i < 20; i++)
{
if (fib[i] <= j)
{
s.insert(sg[j - fib[i]]);
}
}
int t = 0;
while (s.count(t))
t++;
sg[j] = t;
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAl
fibTable();
sgTable();
int m, n, p;
while (~scanf("%d%d%d", &m, &n, &p))
{
if (!m && !n && !p)
break;
int ans = sg[m] ^ sg[n] ^ sg[p];
if (ans)
puts("Fibo");
else
puts("Nacci");
}
return 0;
}
hdu 1536:
sg dfs 模板:
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int a[110];
int sg[maxn];
int n;
int sgDfs(int x)
{
if (sg[x] != -1)
return sg[x];
bool vis[110];
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; i++)
{
if (a[i] <= x)
{
sgDfs(x - a[i]);
vis[sg[x - a[i]]] = true;
}
}
int res = 0;
while (vis[res])
res++;
return sg[x] = res;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAl
while (~scanf("%d", &n) && n)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a + n);
memset(sg, -1, sizeof(sg));
int m;
scanf("%d", &m);
while(m--)
{
int ans = 0;
int k;
scanf("%d", &k);
while (k--)
{
int t;
scanf("%d", &t);
ans ^= sgDfs(t);
}
if (ans)
printf("W");
else
printf("L");
}
printf("\n");
}
return 0;
}