[zoj3591]Nim 游戏
题意:有n堆火柴,选择连续若干堆火柴进行Nim游戏,求让先手胜的选择方案数。
思路:让先手胜等同于这些数的异或值不同于0,不妨转化为求让先手败的方案数。此时记录一个前缀的异或和val[i],那么答案就是count({i,j})(0<=i<j<n,val[i]=val[j])+count(i)(val[i]=0)。直接map统计可能超时,不妨考虑离线做,把val数组sort一下答案就不难得到了,不要忘记最后用总方案数减一下。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt5(name, a, b, c, d, e) name(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0): a(a), b(b), c(c), d(d), e(e) {}
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 3e4 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } int g, s, n, w, a[], nim[]; void init() {
g = s;
rep_up0(i, n) {
a[i] = g;
if (a[i] == ) {
a[i] = g = w;
}
if (g % == ) {
g /= ;
}
else g = (g / ) ^ w;
}
} int main() {
//freopen("in.txt", "r", stdin);
int T;
cin >> T;
while (T --) {
cin >> n >> s >> w;
init();
nim[] = a[];
rep_up0(i, n - ) nim[i + ] = nim[i] ^ a[i + ];
sort(nim, nim + n);
LL ans = ;
rep_up0(i, n) {
if (nim[i] != ) break;
ans ++;
}
LL c = ;
nim[n] = -;
rep_up0(i, n) {
if (nim[i] != nim[i + ]) {
ans += c * (c - ) / ;
c = ;
}
else c ++;
}
cout << (LL)n * (n + ) / - ans << endl;
}
return ;
}
[zoj3591]Nim 游戏的更多相关文章
- Nim游戏
目前有3堆石子,每堆石子个数也是任意的,双方轮流从中取出石子,规则如下:1)每一步应取走至少一枚石子:每一步只能从某一堆中取走部分或全部石子:2)如果谁不能取谁就失败. Bouton定理: 必败状态当 ...
- BZOJ 3105 [CQOI2013]新Nim游戏 ——线性基
[题目分析] 神奇的题目,两人都可以第一次取走足够多堆的石子. nim游戏的规则是,如果异或和为0,那么就先手必输,否则先手有必胜策略. 所以只需要剩下一群异或和为0就可以了. 先排序,线性基扫一遍即 ...
- 【BZOJ-2460&3105】元素&新Nim游戏 动态维护线性基 + 贪心
3105: [cqoi2013]新Nim游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 839 Solved: 490[Submit][Stat ...
- 【BZOJ】3105: [cqoi2013]新Nim游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=3105 题意:k堆火柴,先手和后手在第一次拿的时候都能拿若干整堆火柴(但不能拿完),之后和nim游戏规 ...
- BZOJ3105: [cqoi2013]新Nim游戏 博弈论+线性基
一个原来写的题. 既然最后是nim游戏,且玩家是先手,则希望第二回合结束后是一个异或和不为0的局面,这样才能必胜. 所以思考一下我们要在第一回合留下线性基 然后就是求线性基,因为要取走的最少,所以排一 ...
- 编程之美----NIM游戏
: 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...
- Nim游戏变种——取纽扣游戏
(2017腾讯实习生校招笔试题)Calvin和David正在玩取纽扣游戏,桌上一共有16个纽扣,两人轮流来取纽扣,每人每次可以选择取1个或3个或6个(不允许不取),谁取完最后的纽扣谁赢.Cavin和D ...
- 2014 网选 5011 Game(Nim游戏,数学题)
/* 题意:Nim游戏! 思路:通过异或,判断将n个数表示成二进制的形式之后,是否对应位的数字1 的个数是偶数! */ #include<iostream> using namespace ...
- 【bzoj3150】 cqoi2013—新Nim游戏
www.lydsy.com/JudgeOnline/problem.php?id=3105 (题目链接) 题意 在第一个回合中,第一个游戏者可以直接拿走若干个整堆的火柴.可以一堆都不拿,但不可以全部拿 ...
随机推荐
- lua 逻辑运算 and, or, not
这边并非说lua低级,为了方便区分才这么写的. 高级语言中的逻辑运算符是&&,||,! a&&b : 当a和b都为真, 结果返回为真,当a或者b有一个为假,结果返回为假 ...
- 4.JS跳转路由/刷新/返回页面
1.JS跳转路由(需要拿到父组件的history) clickHandle(){ let history = this.props.history; history.push( '/home') } ...
- 感受python之美,python简单易懂的小例子
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 1 简洁之美 通过一行代码,体会Python语言简洁之美 2 Python ...
- web.config 301
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- Windows 自动登录
https://serverfault.com/questions/840557/auto-login-a-user-at-boot-on-windows-server-2016 Use Sysint ...
- 4.pickling 和unpickling是什么?
pickling 和unpickling是什么? Pickle module accepts any Python object and converts it into a string repre ...
- iterm2终端manpage高亮显示
iterm2 据说是macOS平台上最好用的终端工具,今天下载来用了一下,发现确实很好.但是在使用man命令显示manpage的时候,颜色配置不是很让人满意,就像下面这样: 其实这样也是不错的,但是用 ...
- Eclipse Collections:让Java Streams更上一层楼
\ 关键要点 \\ Eclipse Collections是一个高性能的Java集合框架,为原生JDK集合增加了丰富的功能.\\t Streams是JDK的一个非常受欢迎的功能,但它缺少了一些特性,严 ...
- Linux 下如何产生core文件(core dump设置)
转自:https://blog.csdn.net/star_xiong/article/details/43529637 今天在Linux下调试C程序时,出现段错误,习惯性的ls下当前目录,发现没有生 ...
- 数据结构--栈(附上STL栈)
定义: 栈是一种只能在某一端插入和删除数据的特殊线性表.他按照先进先出的原则存储数据,先进的数据被压入栈底,最后进入的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后被压入栈的,最先弹出).因此栈 ...