首页 > 其他分享 >E. Card Game

E. Card Game

时间:2024-10-16 19:33:08浏览次数:1  
标签:花色 匹配 玩家 player Game Card suit card

E. Card Game

In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.

A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:

  • $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit);
  • $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank).

Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.

Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.

Input

The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).

Additional constraint on the input: $m$ is even.

Output

Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.

Examples

Input

1 4

Output

2

Input

2 2

Output

2

Input

3 6

Output

1690

Input

5 4

Output

568

Input

500 500

Output

84693741

 

解题思路

  参考的官方题解,已经写的很详细了。

  首先因为第 $1$ 种花色可以与任意一种花色匹配,而其余的花色只能同类型匹配。因此玩家一拥有的第 $1$ 种花色牌不少于玩家二,且玩家一拥有的其他花色牌不多于玩家二。

  我们先考虑只有第 $1$ 种花色的匹配方案数。从等级高到低依次分牌。如果将当前牌分给玩家一,那么将其存到待匹配的候选区中。否则将当前牌分给玩家二,我们需要从候选区中选出一张与其匹配,如果候选区中没牌就无法匹配,则这不是一个合法的匹配方案。可以发现这个过程和括号匹配十分相似,把分给玩家一的牌看作是 (,分给玩家二的牌看作是 ),合法的匹配就要求任意一个前缀 ( 的不少于 )。但由于玩家一拥有的第 $1$ 种花色牌可以多于玩家二,因此 $m$ 次匹配后并不要求 ( 的数量恰好等于 ) 的数量。

  因此问题变成了求括号匹配方案数,这个可以用简单 dp 进行求解。定义 $g(i,j)$ 表示匹配了前 $i$ 个位置,且每个前缀的 ( 不少于 ),完成第 $i$ 次匹配后 () 的数量多 $j$ 的方案数。根据第 $i$ 个位置是 () 进行状态划分,转移方程就是$$g(i,j) = g(i-1,j-1) + g(i-1,j+1)$$

  当分完第 $1$ 种花色的牌后,不妨假设玩家一比玩家二多出了 $j$ 张,接下来依次分剩余花色的牌(因为其余花色只能同类型匹配,因此每种花色的分配都是独立的)。这 $j$ 张多出的牌可以与任意剩余花色的任意一张牌匹配,假设第 $i \, (i \geq 2)$ 种花色中有 $k$ 张是与第 $1$ 种花色匹配的,意味着玩家一比玩家二少 $k$  张第 $i$ 种花色的牌。此时我们要考虑有多少种方案使得第 $i$ 种花色的牌互相匹配后,还剩 $k$ 张分给玩家二用于与第 $1$ 种牌匹配。

  同样可以转换成括号匹配的问题,不过此时由于玩家二的牌不少于玩家一,我们定义分给玩家一的牌看作是 ),分给玩家二的牌看作是 (,并且从等级低到高依次分牌。剩下的分析过程和上面的类似,最后合法的匹配方案数就是 $g(m,k)$。

  现在我们要把这 $j$ 张都分给剩余的花色进行匹配,要求这样的方案数还是通过 dp。定义 $f(i,j)$ 表示处理完前 $i$ 种花色的牌后玩家一还多出 $j$ 张第 $1$ 种花色的牌的方案数。根据玩家一用多少张第 $1$ 种花色的牌与玩家二的第 $i$ 种牌匹配进行状态划分,转移方程就是

$$
\begin{cases}
f(i,j) = g(m,j) &,i = 1 \\
f(i,j) = \sum\limits_{k=0}^{m-j}{f(i-1,j+k) \times g(m,k)} &,i \geq 2
\end{cases}
$$

  最后答案就是 $f(n,0)$。

  AC 代码如下,时间复杂度为 $O\left(n m^2\right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 505, mod = 998244353;

int f[N][N], g[N][N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    g[0][0] = 1;
    for (int i = 1; i <= m; i++) {
        for (int j = 0; j <= i; j++) {
            g[i][j] = g[i - 1][j + 1] % mod;
            if (j) g[i][j] = (g[i][j] + g[i - 1][j - 1]) % mod;
        }
    }
    memcpy(f[1], g[m], m + 1 << 2);
    for (int i = 2; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            for (int k = 0; j + k <= m; k++) {
                f[i][j] = (f[i][j] + 1ll * f[i - 1][j + k] * g[m][k]) % mod;
            }
        }
    }
    cout << f[n][0];
    
    return 0;
}

 

参考资料

  Educational Codeforces Round 170 Editorial:https://codeforces.com/blog/entry/135173

标签:花色,匹配,玩家,player,Game,Card,suit,card
From: https://www.cnblogs.com/onlyblues/p/18470598

相关文章

  • 当 smartcardsimulator.dll 加载错误时的解决策略
    smartcardsimulator.dll文件通常与智能卡模拟器或智能卡相关的应用程序有关。智能卡模拟器是一种软件工具,用于模拟智能卡的行为,以便开发人员测试和调试智能卡应用。smartcardsimulator.dll文件负责处理智能卡模拟的相关功能。当您看到“smartcardsimulator.dll加载错误”......
  • Solution - Codeforces 1628D2 Game on Sum (Hard Version)
    首先来考虑Easy。注意到的是最后输出的答案要求是模意义下的,这说明对于实数二分的做法都已经用不了了。注意到\(n,m\le3000\)的数据范围,于是一个想法就是考虑DP之类的算法。考虑到B选了\(+/-\)实际上就代表着下一轮的\(m\)是否会\(-1\),于是可以设状态为\(f_{i......
  • Educational Codeforces Round 170 (Rated for Div. 2) C. New Games
    题意转化找一些相邻的数(其中相邻定义为递增序下任意相邻两数差\(\leq1\))求相邻数中,不同数字有\(k\)种,取到数字个数的最大值算法容易想到按顺序排列观察到有点像滑动窗口,考虑用队列维护一个出现不同数字次数为\(k\)的区间,再计算代码来自转载地址voidsolv......
  • 题解:P11145 Strange Homura Game
    ProblemLinkStrangeHomuraGame题意让你猜测一个数\(n\),你只能输出两次,每次输出一个数\(x\),返回\(x\bmodn\)。Solution令输入的数为\(A,B\),输出的数为\(a,b\),答案为\(n\)。一开始想的是CRT,但只能询问\(2\)次。发现输入的值是经过\(\bmodn\)的,已知\((A-a)......
  • 0xGame2024-week1-crypto
    CryptoCaesarCipher密文:0yHbnf{Uif_Cfhjoojoh_Pg_Dszqup}提示:凯撒加密。改成-1就好了RSA_EasyfromCrypto.Util.numberimportbytes_to_long,getPrimefromhashlibimportmd5fromrandomimportrandintfromgmpy2importinvert,gcd#HashFunction:defMD5(m......
  • 0xGame2024WP
    Contents[Week1]Misc[Week1]0xGame2048题目:通过一点也不可靠的途径,我们提前截获了0xGame2048的题目,据说这就是那时候的base编码(?0xGame{W3lc0me_t0_0xG4me!!!}是base2048加密,使用对应的解密,base2048在线解密站点[Week1]加密的压缩包?压缩包看起......
  • P11073 Game King
    P11073GameKing-洛谷|计算机科学教育新生态(luogu.com.cn)缩点,分别重建图,再建反图,可知,拓扑序大的一定不能到拓扑序小的。不断新建点统计。#include<iostream>#include<algorithm>#include<cstring>#include<unordered_map>#include<queue>usingnamespacestd......
  • Leetcode 贪心算法之Jump Game II
    题目描述给定一个长度为n的0索引整数数组nums。初始位置为nums[0]。每个元素nums[i]表示从索引i向前跳转的最大长度。换句话说,如果你在nums[i]处,你可以跳转到任意nums[i+j]处:0<=j<=nums[i]i+j<n返回到达nums[n-1]的最小跳跃次数。生成的......
  • INFO1113 / COMP9003 a prototype of the game
    INFO1113/COMP9003AssignmentDue:20October2024,11:59PMAESTThisassignmentisworth20%ofyourfinalgrade.TaskDescriptionInthisassignment,youwillcreateagameintheJavaprogramminglanguageusingtheProcessinglibraryforgraphics......
  • pygame写一个黑客帝国屏保
    代码:#coding=utf-8importos,sys,re,timeimportpygameimportrandomfromwin32apiimportGetSystemMetricsfromtkinterimportmessagebox#pyinstaller-F-wpygame_heike.pypygame.init()pygame.display.set_caption("黑客帝国屏幕保护")percent=1scr......