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. [WC2007]剪刀石头布——费用流

    比较有思维含量的一道题 题意:给混合完全图定向(定向为竞赛图)使得有最多的三元环 三元环条件要求比较高,还不容易分开处理. 正难则反 考虑,什么情况下,三元组不是三元环 一定是一个点有2个入度,一个点 ...

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

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

  3. Android-使用ViewFlipper实现轮番切换广告栏

    所谓的轮番切换广告栏,指的是下面这个东西,笔主不知道该怎么确切描述这货... 笔主没有百度研究过其他大牛是怎么实现这个功能的,在这里笔主充分发挥DIY精神,利用ViewFlipper闭门土制了一个,下 ...

  4. OpenJudge百炼-2747-数字方格-C语言-枚举

    描述:如上图,有3个方格,每个方格里面都有一个整数a1,a2,a3.已知0 <= a1, a2, a3 <= n,而且a1 + a2是2的倍数,a2 + a3是3的倍数, a1 + a2 ...

  5. Tile Cut~网络流入门题

    Description When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like t ...

  6. git 的证书重新设置,以及如何让git 记住提交的用户名和密码

    1.git 的证书的重新设置的命令是: git config --system --unset credential.helper 2.保存git的用户名和密码注意这里是全局保存 git config ...

  7. idea设置文件的编码格式

    在打开某些类时会发现注释是乱码的,该如何解决idea的文件乱码呢?这就需要设置这个文件的合适编码格式: idea设置文件编码的两种方式分别如下: 第一种方式点击idea的右下角的图标如下图所示: 第二 ...

  8. 精通javascript笔记(智能社)——数字时钟

    JS代码: <script type="text/javascript">    window.onload=function(){ //小于10的数字补零及数字转字符 ...

  9. 关于MyBatis的collection集合中只能取到一条数据的问题

    问题:在涉及多表查询的时候,使用collection元素来映射集合属性时,出现了只能查询到一条数据的情况,但用sql语句在数据库中查询会有多条记录. 解决:如果两表联查,主表和明细表的主键都是id的话 ...

  10. 动态规划:状压DP-斯坦纳树

    最小生成树是最小斯坦纳树的一种特殊情况 最小生成树是在给定的点集和边中寻求最短网络使所有点连通 而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开销最小 BZOJ2595 题意是给定一个棋盘 ...