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 ...
随机推荐
- [学习笔记]对未来做出承诺的DP小结
这是一种DP状态设计方法. 有些题,当你必须以一个顺序往后填的话,然而后面的填法会对之前产生影响,那么,不妨在之前就对未来怎么填做出承诺. 通俗的讲,就是对未来打一个表. 然后后面填的时候,直接查表转 ...
- linux启动一个web项目时验证码不能出现的问题的解决
解决方法: 在tomcatcatalina.sh中搜索cygwin=false,然后在它的上面加上这句话JAVA_OPTS='-Djava.awt.headless=true' 具体参考如下图片: 然 ...
- maven 压缩、合并 js, css
转载自:http://blog.csdn.net/fangxing80/article/details/17639607 我们知道在 Web 应用开发中为了提高客户端响应速度,需要将页面使用的资源最小 ...
- elementUi 组件--el-table
[需求]在element中,将表格中的数据进行处理,然后渲染出来.例如,将数据保留小数点后两位显示. [知识点]formatter:用来格式化内容 [分析]在element 的table中,实现的过程 ...
- 计算机网络中七层,五层,四层协议;IP 地址子网划分
七层协议: 7 应用层(http) 6 表示层(上层用户可以相互识别的数据:jpg) 5 会话层(不同主机不同线程间的通信) 4 运输层(tcp/ip:传输层提供端到端的透明数据服务)/差错控制和流量 ...
- 【洛谷 P1631】 序列合并 (堆)
题目链接 直接暴力搞是\(n\)方的复杂度.\(n^2\)个数选\(n\)个最小的,容易想到堆. 我们堆里记录两个信息:到\(A\)数组哪个位置了,到\(B\)数组哪个位置了, 我直接把这两个信息存在 ...
- bzoj 1025 DP
这道题根据群论的基础知识,我们可以转化成将n拆分成若干数,求这些数 的lcm的方案数 先筛下素数表prime 那么我们可以用DP来解决这个问题,用W[I,J]代表I这个数,拆成若干个数, 其中质因数最 ...
- 【mysql优化1】表的优化与列类型选择
数据类型及字节数参考http://www.cnblogs.com/qlqwjy/p/8590639.html -------------------------表的优化:--------------- ...
- Appium+python自动化29-toast消息【转载】
本篇转自博客:上海-悠悠 前言 appium1.5以后的版本才支持toast定位,并且 'automationName'得设置为'Uiautomator2',才能捕获到. 一. Supported P ...
- seneca的一段代码(原创)
var seneca=require('seneca')() seneca.add({cmd:'wordcount'},function(msg,respond){ var length=0; if( ...