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. Win7命令mklink的使用

    C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了AppData,Chrome占了1G多的空间. 当时安装Chrome浏览器时因为不能指定安装目录,所以Chrome ...

  2. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  3. python 闭包与装饰器

    1.闭包--返回子函数名 作用:使用子函数之外的父函数的变量 闭包就是你调用了一个函数a,这个函数a反悔了一个子函数名b,这个返回的函数b就叫做闭包 代码举例 def a(): test = 'aa' ...

  4. windows10-seaslog安装笔记

    1.seasLog在windows下的安装 首先,要下载seasLog的dll文件,下载地址:http://pecl.php.net/package/SeasLog  选择对应你的系统和php版本的d ...

  5. 转:java 帐号激活与忘记密码 实例

    原文链接:http://endual.iteye.com/blog/1613679 一.帐户激活   在 很多时候,在某些网站注册一个用户之后,网站会给这个用户注册时填写的email地址发送一封帐户激 ...

  6. oracle有关游标的知识

    一:前言 今天我自己第二次写游标,我擦,觉得自己在数据库方面需要很大的提高啊.今天遇到三个问题,第一个是oracle数据库中的数据拆分的问题,这个我用regexp_substr来进行解决,第二个问题就 ...

  7. CentOs7安装JDK/Tomcat/Git/Gradle

    安装Jdk: wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/j ...

  8. [bzoj3238][Ahoi2013]差异——后缀自动机

    Brief Description Algorithm Design 下面给出后缀自动机的一个性质: 两个子串的最长公共后缀,位于这两个串对应的状态在parent树上的lca状态上.并且最长公共后缀的 ...

  9. 【Python实例二】BeautifulSoup爬虫简单实践

    前言 前面安装了BeautifulSoup库,现在就来实现一下吧. 目录 一.Urllib库的使用 二.BeautifulSoup的使用 三. 一个示例 ----------------------- ...

  10. python访问网站

    #!/usr/bin/env python # encoding: utf-8 from functools import wraps import requests from lxml import ...