【ZOJ - 3591】Nim(博弈问题,思维,STLmap)

题干:

Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. Here is another version of Nim game. There are N piles of stones on the table. Alice first chooses some CONSECUTIVE piles of stones to play the Nim game with Tom. Also, Alice will make the first move. Alice wants to know how many ways of choosing can make her win the game if both players play optimally.

You are given a sequence a[0],a[1], ... a[N-1] of positive integers to indicate the number of stones in each pile. The sequence a[0]...a[N-1] of length N is generated by the following code:

int g = S; 

for (int i=0; i<N; i++) { 

    a[i] = g;

    if( a[i] == 0 ) { a[i] = g = W; }

    if( g%2 == 0 ) { g = (g/2); }

    else           { g = (g/2) ^ W; }

}

Input

There are multiple test cases. The first line of input is an integer T(T ≤ 100) indicates the number of test cases. Then T test cases follow. Each test case is represented by a line containing 3 integers NS and W, separated by spaces. (0 < N≤ 105, 0 < S, W ≤ 109)

Output

For each test case, output the number of ways to win the game.

Sample Input

2
3 1 1
3 2 1

Sample Output

4
5

题目大意:

   因为我们知道Nim博弈,这x堆石子的异或和为0则必败,不为零则必胜,所以就是问你在这些石子中选择哪些连续的堆,可以必胜,问你选择方案数。

  也就是,给n个数,让你求,有多少段连续区间,使得这段区间的异或和不为0。

解题报告:

正难则反,可以先求出有多少个区间,异或和为零,然后用总区间数去作差就行了。 

做法就是个常见的在线处理。。。这题也可以先求出这个和来,然后用C(n,2)去减这个和,得到的就是答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
int n;
ll a[100010];
ll sum[100010];
map<ll,int> mp;
int main() {int t,q,cnt;ll i,j,k,g,S,W,res,tmp,ss;cin>>t;while(t--) {cin>>n>>S>>W;g = S;res=0;for (i=1; i<=n; i++) {a[i] = g;if( a[i] == 0)a[i] = g = W;if( g%2 == 0 )g = (g/2);else g = (g/2) ^ W;}mp.clear();mp[0]=1;for(i=1; i<=n; i++) {sum[i]=sum[i-1]^a[i];res+=i-mp[sum[i]];mp[sum[i]]++;}cout<<res<<endl;}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440585.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【牛客 - 369B】小A与任务(贪心,优先队列)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/B 来源&#xff1a;牛客网 小A手头有 n 份任务&#xff0c;他可以以任意顺序完成这些任务&#xff0c;只有完成当前的任务后&#xff0c;他才能做下一个任务 第 i 个任务需要花费 xixi 的时间…

word硬回车是怎么产生的_在word中怎样删除软硬回车?

1.点击“开始”&#xff0d;“程序”&#xff0d;“附件”&#xff0d;“记事本”&#xff0c;打开“记事本”&#xff0c;放在一边。2.打开需要调整的Word文档&#xff0c;在文章的任意处单击一下鼠标左键。然后按住键盘的“Ctrl”键不要松&#xff0c;再按一下“A”键&#x…

*【POJ - 3659】Cell Phone Network (树形dp,最小支配集)

题干&#xff1a; Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so the…

测试jdbc连mysql数据库_java连接mysql数据库及测试是否连接成功的方法

本文实例讲述了java连接mysql数据库及测试是否连接成功的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;package com.test.tool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import…

mysql配置日志老化配置_mysql配置-日志大小限制和自动删除

线上的项目磁盘消耗问题, 发现和MySQL日志有关系.需要处理的问题如何限制大小 不让日志无限膨胀?配置日志不留?删除的方式和直接删除会对服务有什么影响?解决方式限制大小, 保留最近一段时间日志.set global expire_logs_days7; # 命令行进入MySQL中, 临时设置保留最近7天日…

【HDU - 5091】Beam Cannon(线段树,扫描线)

题干&#xff1a; Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on t…

mysql的传播特性_spring事务传播特性和mysql事务隔离级别

spring事务的传播特性--7种REQUIRED支持当前事务&#xff0c;如果没有事务会创建一个新的事务SUPPORTS支持当前事务&#xff0c;如果没有事务的话以非事务方式执行MANDATORY(强制性)支持当前事务&#xff0c;如果没有事务抛出异常REQUIRES_NEW创建一个新的事物并挂起当前事务NO…

【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)

题干&#xff1a; Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently. Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both si…

【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配)

题干&#xff1a; Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a number K. 2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube i…

qt同时连接oracle和mysql_QT连接Oracle和Mysql的详细步骤,已成功~!

近几天一直在整QT的数据库连接这一块。因为QT是开源的&#xff0c;所以涉及的连接Oracle及Mysql的驱动都必须自己编译生成。通过不断的测试、调试&#xff0c;终于把QT连接Oracle和Mysql的驱动编译生成好了。QT环境&#xff1a;Qt 4.6.0打开Qt Command Prompt&#xff0c;开始菜…

【POJ - 2632】Crashing Robots(模拟)

题干&#xff1a; In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occup…

一台linux上运行多个mysql_linux下同时运行多个mysql

来自网络&#xff0c;感谢开源&#xff0c;感谢分享通过rpm安装mysql&#xff0c;测试版本5.1.481、在linux下通过:#useradd multi -g mysql -s /sbin/nologin添加一个multi用户&#xff0c;并加入到mysql组#passwd multi给multi用户添加密码&#xff0c;这里设置的密码为multi…

【SPOJ - QTREE2】Query on a tree II(LCA,倍增)

题干&#xff1a; You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of the foll…

pandownload用户未登录_Pandownload再度复活,下载速度飙升到10MB/s以上

PanDownload再度复活版&#xff0c;此版镜像服务器由新城旧梦维护卢本伟修改版&#xff0c;基本都是维持在10M/S的下载速度&#xff0c;如果你的宽带够大&#xff0c;下载速度会更快&#xff0c;无需安装即可免费使用&#xff0c;但是需要登陆才能下载。(Pandownload卢本伟修改…

【蓝桥杯 - 试题】立方尾不变(tricks,快速取出一个数字的后n位)

题干&#xff1a; 有些数字的立方的末尾正好是该数字本身。 比如&#xff1a;1,4,5,6,9,24,25,.... 请你计算一下&#xff0c;在10000以内的数字中&#xff08;指该数字&#xff0c;并非它立方后的数值&#xff09;&#xff0c;符合这个特征的正整数一共有多少个。 请提交该…

momentjs转换格式_Moment.js+Vue过滤器的使用,各种时间格式转换为YYYY-MM-DD HH:mm:ss格式...

前言这篇文章将Moment.js与vue过滤器连用。如果不会过滤器的朋友&#xff0c;可以先看这篇文章vue过滤器一、Moment.js是什么&#xff1f;Moment.js是JavaScript 日期处理类库。使用场景&#xff1a;vue项目中经常需要将时间戳转换为各种时间格式再显示。二、使用步骤1.安装这里…

【HDU - 1943】Ball bearings(几何问题)

题干&#xff1a; The Swedish company SKF makes ball bearings. As explained by Britannica Online, a ball bearing is “one of the two types of rolling, or anti friction, bearings (the other is the roller bearing). Its function is to connect two machine mem…

mysql显示修改密码_MySQL修改密码

第一种方式&#xff1a;最简单的方法就是借助第三方工具Navicat for MySQL来修改&#xff0c;方法如下&#xff1a;1、登录mysql到指定库&#xff0c;如&#xff1a;登录到test库。2、然后点击上方“用户”按钮。3、选择要更改的用户名&#xff0c;然后点击上方的“编辑用户”按…

【POJ - 2486】Apple Tree (树形背包,dp)

题干&#xff1a; Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the no…

mysql 磁盘组_有效管理 ASM 磁盘组空间

ORA-15041: diskgroup space exhausted 对您的数据库环境的直接和间接影响&#xff1f;与 ASM 磁盘组相关的磁盘空间问题和 ORA-15041 错误会ORA-15041: diskgroup space exhausted 对您的数据库环境的直接和间接影响&#xff1f;与 ASM 磁盘组相关的磁盘空间问题和 ORA-15041 …