Codechef A Game With a Sheet of Paper
Discription
Yuuko and Nagi like to play the following game:
Initially they take a checkered sheet of paper of the size of N x M cells. Then, one cell, let's call this cell (X, Y) is marked. Then, they take the moves alternately. A move consists of making a long horizontal or vertical cut. This cut should coincide with one of the lines that divide the sheet into cells. Then, a part that doesn't contain a marked cell is thrown away and another player takes her turn. A player who is unable to make a turn because after the opponent's turn the dimensions of the sheet became equal to 1 x 1 loses. Yuuko plays first.
Yuuko and Nagi have played a lot of games together so now they both know the optimal strategy and always use it. Today girls are going to play a game on a N x M cells sheet of paper, but they haven't yet decided, what is the cell (X, Y) to be marked. Yuuko is interested, in how many ways it's possible to choose this cell so the she can become a winner, regardless of Nagi's moves.
Input
The first line of input consists of an integer T - the number of test cases. Then, Tlines describing the test cases follow. The i-th such line contains two integers Nand M, separated by a single space.
Output
For each test case, output a single line containing the number of ways to choose the marked cell in order to ensure Yuuko's win.
Example
Input:
2
5 8
6 7 Output:
40
42
Scoring
T = 100, 1 <= N, M <= 10 : 25 points.
T = 100, 1 <= N, M <= 1000 : 36 points.
T = 100, 1 <= N, M <= 106 : 39 points.
至于中文题面。。。
Mandarin Chinese
假设我们选了(x,y)点,那么它上面有x-1行,下面有n-x行,左边有y-1列,右边有m-y列。
而每次切割相当于让这四个量中的一个减小任意(但必须保证操作后是非负数)
这不就是Nim游戏吗???
但是光看出这个还不行,因为题目还要快速的算方案数。
先手必胜的情况下SG函数的Nim和是不为0的,这个不太好算,我们不妨算一下后手必胜的方案,再用总方案减去这个就是答案(总方案数显然是n*m)
然后发现其实并不是完全意义上的4堆石子,因为行的两堆和列的两堆是有相互限制的。
我们要让4堆石子的SG函数的Nim和为0,那就让行的Nim和和列的Nim和相等即可,那么我们就用a[i]记录行的Nim和==i的有多少组,
然后找列的时候累加a[i^(m-1-i)]即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
#define maxn 1000005
using namespace std;
ll tot,n,m;
int T,a[maxn]; int main(){
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&n,&m);
tot=n*m;
n--,m--; if(n>m) swap(n,m);
for(int i=;i<=n;i++) a[i^(n-i)]++;
for(int i=;i<=m;i++) tot-=(ll)a[i^(m-i)];
printf("%lld\n",tot); if(T) memset(a,,sizeof(a));
} return ;
}
Codechef A Game With a Sheet of Paper的更多相关文章
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- Appleman and a Sheet of Paper
题意: 给一纸条,两种操作: 1.将左侧长度为$x$的纸条向右翻折. 2.询问位于$[l,r]$的纸条总长度. 解法: 考虑启发式,每一次一个小纸条折叠我们可以看做是一次合并,如果我们每一次将较小的纸 ...
- 如何写出优秀的研究论文 Chapter 1. How to Write an A+ Research Paper
This Chapter outlines the logical steps to writing a good research paper. To achieve supreme excelle ...
- CF Playing with Paper
Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #296 (Div. 2) A. Playing with Paper
A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an o ...
- Uva - 177 - Paper Folding
If a large sheet of paper is folded in half, then in half again, etc, with all the folds parallel, t ...
- TOJ 2541: Paper Cutting
2541: Paper Cutting Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: ...
- Codeforces Round #587 (Div. 3) C. White Sheet
链接: https://codeforces.com/contest/1216/problem/C 题意: There is a white sheet of paper lying on a rec ...
- Codeforces Round #344 (Div. 2) B. Print Check
B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- ContestHunter#17-C 舞动的夜晚
Description: L公司和H公司举办了一次联谊晚会.晚会上,L公司的N位员工和H公司的M位员工打算进行一场交际舞.在这些领导中,一些L公司的员工和H公司的员工之间是互相认识的,这样的认识关系一 ...
- 【BZOJ 2324】[ZJOI2011]营救皮卡丘 费用流
本人实行诱骗拐卖(利用自然分层与实际意义),正解拼接补充(充分利用最大流限制(不浪费任何一个走出去的机会而不是不浪费任何一个已有的流)与问题转换) #include <cstdio> #i ...
- CodeForces743E. Vladik and cards 二分+状压dp
这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有 ...
- 毕业答辩的PPT攻略
关于内容: 1.一般概括性内容:课题标题.答辩人.课题执行时间.课题指导教师.课题的归属.致谢等. 2.课题研究内容:研究目的.方案设计(流程图).运行过程.研究结果.创新性.应用价值.有关课题延续 ...
- bzoj 4880 [Lydsy1705月赛]排名的战争 贪心
[Lydsy1705月赛]排名的战争 Time Limit: 8 Sec Memory Limit: 256 MBSubmit: 338 Solved: 69[Submit][Status][Di ...
- VC关于置顶窗口的方法小结
转摘自:http://blog.csdn.net/wirror800/article/details/4002381 将窗体置顶的方法有: //将窗体置顶的API函数 ::SetWindowPos(m ...
- sublime2创建一个html5的snippets文件
背景:跟了一个网上课程,老师哗啦啦敲代码,屏幕上只敲了几个字,键盘一操作,瞬间一大溜代码,看得我心惊肉跳连忙暂停抄抄抄. 举个简单的例子,我需要创建一个html文件.但是我不想每次都敲固定的格式.那么 ...
- nodejs是用来做什么的?
有些人说“这是一种通过javascript语言开发web服务端的东西”.更直白的可以理解为:node.js有非阻se塞,事件驱动/O等特性,从而让高并发(high concurrency)在的轮询和c ...
- spring的普通类中如何取session和request对像
在使用spring时,经常需要在普通类中获取session,request等对像. 比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts ...
- linux基础-临时和永久修改ip地址以及通配符相关
一.临时配置网络(ip,网关,dns) 修改临时ip地址: 1.ifconfig查看当前的网卡和ip地址 2.临时修改IP地址:ifconfig ens32 192.168.16.200/24,ifc ...