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 (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=0;i<=n;i++) a[i^(n-i)]++;
        for(int i=0;i<=m;i++) tot-=(ll)a[i^(m-i)];
        printf("%lld\n",tot);

        if(T) memset(a,0,sizeof(a));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/JYYHH/p/8361454.html

时间: 2024-10-19 04:52:43

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 rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have

Codeforces 461C Appleman and a Sheet of Paper(模拟)

题目链接:Codeforces 461C Appleman and a Sheet of Paper 题目大意:就是一个叠被子的过程,穿插着询问一段区间上被子的单位厚度. 解题思路:用前缀和数组模拟即可.因为对于折超过一半的处理为将令一半叠上来,所以需要变量记录当前被子的正反状态.处理好下标关系即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

Codeforces 461C. Appleman and a Sheet of Paper

每次只把短的部分往长的部分折叠,直接用树状数组爆搞就可以了. 每次长度都缩小一些暴力的复杂度不是太高,启发式暴力???? C. Appleman and a Sheet of Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman has a very big sheet of paper. This s

461C. Appleman and a Sheet of Paper(树状数组)

C. Appleman and a Sheet of Paper time limit per test 2 seconds memory limit per test 256 megabytes Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a she

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

题目地址:http://codeforces.com/contest/461/problem/C 题目大意:见原题. 算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记.注意细节. Code: #include <cstdio> #include <algorithm> #define N 100000 using namespace std; bool rev; int n,q,beg=1,end,typ,p,l,r,bit[N+10]; inline v

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 origami from a rectangular a mm ?×? b mm sheet of paper (a?>?b). Usually the first step in making an origami is making a square piece of paper from the

如何写出优秀的研究论文 Chapter 1. How to Write an A+ Research Paper

This Chapter outlines the logical steps to writing a good research paper. To achieve supreme excellence or perfection in anything you do, you need more than just the knowledge. Like the Olympic athlete aiming for the gold medal, you must have a posit

Codeforces Round #296 (Div. 2)——A long long——Playing with Paper

One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm  ×  b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by

Codeforces Round #587 (Div. 3) C - White Sheet

原文链接:https://www.cnblogs.com/xwl3109377858/p/11564279.html Codeforces Round #587 (Div. 3) C - White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you w