# T678256 多米诺游戏 ## 题目背景 Alice 和 Bob 在一个 $n$ 行 $m...

创建于:2025年10月11日

使用 GPT-5 Thinking 回答 Chat01

问题

T678256 多米诺游戏

题目背景

Alice 和 Bob 在一个 nnmm 列的网格上玩游戏,他们轮流放置 1×21 \times 2 的多米诺骨牌(可以横放或竖放),Bob 先手。

Alice 的目标是尽量让网格被放满,Bob 的目标是尽量让网格不被放满。

骨牌不能重叠,也不能超出网格边界。

当网格被完全放满时,输出 Yes;如果网格没有被放满,但双方都无法再放置骨牌时,输出 No

现在给定多组数据,请你判断每组数据的游戏结果。

题目描述

给定 TT 组数据,每组数据包含两个整数 nnmm,表示网格的行数和列数。

Alice 和 Bob 轮流在网格上放置 1×21 \times 2 的多米诺骨牌,Bob 先手。

  • 如果最终网格被完全放满,输出 Yes

  • 如果网格没有被放满,但双方都无法再放置骨牌,输出 No

输入格式

第一行一个整数 TT,表示数据组数。

接下来 TT 行,每行两个整数 n,mn, m

输出格式

TT 行,每行一个字符串 YesNo,表示对应数据的游戏结果。

输入输出样例 #1

输入 #1

text
3 1 2 2 2 3 3

输出 #1

text
Yes Yes No

说明/提示

样例解释

  • 对于 1×21 \times 2 的网格,Bob 可以放一个骨牌占满整个网格,因此输出 Yes

  • 对于 2×22 \times 2 的网格,Bob 先放一个骨牌(横放或竖放),Alice 可以在剩余空间放另一个骨牌,最终网格被放满,输出 Yes

  • 对于 3×33 \times 3 的网格,总格子数为奇数,无法用 1×21 \times 2 的骨牌完全覆盖,因此最终网格不会被放满,输出 No

数据范围

1T1051 \le T \le 10^5,1n,m10181 \le n, m \le 10^{18}。(请使用c++)

思考

Considering game strategy

I’m thinking through the rules for the domino game. Bob and Alice alternate turns, with Bob trying to prevent full tiling and Alice trying to complete it. The game is based on whether the total number of squares, N, is even, since a complete tiling only works under those conditions. I'll work through optimal strategies.

Exploring tiling and game dynamics

For any rectangle with an even area, domino tiling exists if at least one dimension is even. Example: a 2x3 grid (N=6) can be tiled. The tricky part is whether Bob can prevent full tiling in an adversarial game. It might depend on how the game alternates, often influenced by parity.

Analyzing the game dynamics

This game is comparable to maximal domino packing, where the final number of dominoes matches the maximal matching. The tricky part is whether a player can reduce the number of dominoes by placing them poorly. The first player aims to prevent full tiling, but can the second player guarantee success with a perfect strategy?

Considering second player strategy

When the board has an even number of cells, Alice (the second player) can potentially use a "pairing strategy" by partitioning the grid into disjoint dominoes. This way, when Bob places a domino, Alice can place the corresponding paired domino, filling the board completely using symmetry.

Finalizing the tiling strategy

When placing dominoes, if one dimension is even, the second player can use symmetry to mirror Bob's moves and ensure full tiling. If N (n*m) is odd, Bob can prevent filling by parity, but if N is even and one dimension is even, the second player can ensure full tiling using this strategy.

Thought for 35s

回答

结论很简单:

  • 如果 nnmm 中至少有一个是偶数,答案是 Yes
  • 若二者都为奇数(总格数 n×mn\times m 为奇数),答案是 No

证明思路

  • 二者都奇数:总格数为奇数,不可能被 1×21\times2 骨牌完全覆盖,必为 No
  • 至少一者为偶数:设有一维为偶数(例如 mm 为偶),Alice 采用镜像策略:以棋盘的竖直中轴为对称轴,Bob 下在哪两格,Alice 就在对称位置放置同样形状的骨牌。这样每一轮两人合起来恰好覆盖一对对称位置,最终能把棋盘铺满,故为 Yes

代码(C++,O(T))

cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; if (!(cin >> T)) return 0; while (T--) { long long n, m; cin >> n >> m; // 只需判断奇偶,不必相乘避免溢出 if ((n & 1LL) && (m & 1LL)) { cout << "No\n"; } else { cout << "Yes\n"; } } return 0; }

说明:只判断奇偶即可,避免 n×mn \times m 溢出;范围 n,m1018n,m \le 10^{18}long long 足够进行奇偶判断。

分享这个问答