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 (XY) 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 (XY) 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 <= NM <= 10 : 25 points.

T = 100, 1 <= NM <= 1000 : 36 points.

T = 100, 1 <= NM <= 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的更多相关文章

  1. 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 ...

  2. Appleman and a Sheet of Paper

    题意: 给一纸条,两种操作: 1.将左侧长度为$x$的纸条向右翻折. 2.询问位于$[l,r]$的纸条总长度. 解法: 考虑启发式,每一次一个小纸条折叠我们可以看做是一次合并,如果我们每一次将较小的纸 ...

  3. 如何写出优秀的研究论文 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 ...

  4. CF Playing with Paper

    Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 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 ...

  6. 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 ...

  7. TOJ 2541: Paper Cutting

    2541: Paper Cutting  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【NOIP模拟赛】天神下凡 动态开点线段树

    这些圆一定是在同一水平面上的,由于他们没有相交,因此我们发现他们每个人与外界关系可以分为,1.存在并圈圈 2.存在圈圈并被割,因此我们把所有的圆都加1,把被割的在加1,就可以啦,因此我们开一个线段树, ...

  2. 微信小程序使用Socket

    首先,一个小程序同时只能有一个WebSocket连接,如果当前已经存在一个WebSocket连接,会关闭当前连接,并重新建立一个连接. 其次,如果使用了appID,协议必须是 wss://... 最近 ...

  3. BZOJ 2457 双端队列(思维

    2457: [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 582  Solved: 253[Submit][Sta ...

  4. 用DOM解析XML ,用xpath快速查询XML节点

    XPath是一种快速查询xml节点和属性的一种语言,Xpath和xml的关系就像是sql语句和数据库的关系.用sql语句可以从数据库中快速查询出东西同样的用xPath也可以快速的从xml中查询出东西. ...

  5. 数据仓库3级范式(3NF)基础

    一.引言 最近在整理理大数据模式下的数据仓库数据模型,资料来自互联网和读过的数据仓库理论和实践相关. 二.3NF (1)1NF-无重复的列 数据库表的每一列都是不可分割的基本数据项,同一列中不能有多个 ...

  6. 解读dbcp自动重连那些事

    转载自:http://agapple.iteye.com/blog/791943 可以后另一篇做对比:http://agapple.iteye.com/blog/772507 同样的内容,不同的描述方 ...

  7. PHP 扒一扒这些题目都考了哪些知识点

    1.模除 题目: <?php echo -10%3; *结果* -1 分析:其实这道题的知识点是在考模除和正负号的关系,那么我们看一段进阶的代码 <?php echo "10%3 ...

  8. 【BZOJ3237】【AHOI2013】连通图 [CDQ分治]

    连通图 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Sampl ...

  9. The service base of EF I am using

    using CapMon.Data; using System; using System.Collections.Generic; using System.Linq; using System.T ...

  10. mongo语法和mysql语法对比学习

    我们总是在对比中看到自己的优点和缺点,对于mongodb来说也是一样,对比学习让我们尽快的掌握关于mongodb的基础知识. mongodb与mysql命令对比 关系型数据库一般是由数据库(datab ...