CF1648C.Tyler and Strings

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

While looking at the kitchen fridge, the little boy Tyler noticed magnets with symbols, that can be aligned into a string ss.

Tyler likes strings, and especially those that are lexicographically smaller than another string, tt. After playing with magnets on the fridge, he is wondering, how many distinct strings can be composed out of letters of string ss by rearranging them, so that the resulting string is lexicographically smaller than the string tt? Tyler is too young, so he can't answer this question. The alphabet Tyler uses is very large, so for your convenience he has already replaced the same letters in ss and tt to the same integers, keeping that different letters have been replaced to different integers.

We call a string xx lexicographically smaller than a string yy if one of the followings conditions is fulfilled:

  • There exists such position of symbol mm that is presented in both strings, so that before mm-th symbol the strings are equal, and the mm-th symbol of string xx is smaller than mm-th symbol of string yy.
  • String xx is the prefix of string yy and x≠yx \neq y.

Because the answer can be too large, print it modulo 998 244 353998\,244\,353.

当小男生泰勒看着厨房冰箱时,注意到上面贴着一些印有符号的磁贴,这些磁贴可以排列成一个字符串 ss。

泰勒很喜欢字符串,尤其喜欢那些字典序小于另一个给定字符串 tt 的字符串。在冰箱上摆弄这些磁贴后,他开始思考:将字符串 ss 中的字符重新排列(即重排其字母),一共能组成多少个互不相同的字符串,使得得到的字符串字典序严格小于字符串 tt?泰勒年纪太小,还无法回答这个问题。泰勒所用的字母表非常大,因此为方便起见,他已将 ss 和 tt 中相同的字母分别替换为相同的整数,且不同字母被替换为不同的整数。

我们称字符串 xx 字典序小于字符串 yy,当且仅当满足以下任一条件:

  • 存在某个位置 mm(该位置在两个字符串中均有效),使得 xx 与 yy 在第 mm 位之前的所有字符完全相同,且 xx 的第 mm 个字符严格小于 yy 的第 mm 个字符;
  • 字符串 xx 是字符串 yy 的真前缀(即 xx 是 yy 的前缀且 x≠yx \neq y)。

由于答案可能非常大,请将结果对 998 244 353998\,244\,353 取模后输出。

输入格式

The first line contains two integers nn and mm (1≤n,m≤200 0001 \le n, m \le 200\,000) — the lengths of strings ss and tt respectively.

The second line contains nn integers s1,s2,s3,…,sns_1, s_2, s_3, \ldots, s_n (1≤si≤200 0001 \le s_i \le 200\,000) — letters of the string ss.

The third line contains mm integers t1,t2,t3,…,tmt_1, t_2, t_3, \ldots, t_m (1≤ti≤200 0001 \le t_i \le 200\,000) — letters of the string tt.

第一行包含两个整数 nn 和 mm(1≤n,m≤200 0001 \le n, m \le 200\,000),分别表示字符串 ss 和 tt 的长度。

第二行包含 nn 个整数 s1,s2,s3,…,sns_1, s_2, s_3, \ldots, s_n(1≤si≤200 0001 \le s_i \le 200\,000),表示字符串 ss 的各个字符。

第三行包含 mm 个整数 t1,t2,t3,…,tmt_1, t_2, t_3, \ldots, t_m(1≤ti≤200 0001 \le t_i \le 200\,000),表示字符串 tt 的各个字符。

输出格式

Print a single number — the number of strings lexicographically smaller than tt that can be obtained by rearranging the letters in ss, modulo 998 244 353998\,244\,353.

输出一个整数——即通过重新排列字符串 ss 中的字母所能得到的、字典序小于 tt 的字符串的个数,对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    3 4
    1 2 2
    2 1 2 1

    输出#1

    2
  • 输入#2

    4 4
    1 2 3 4
    4 3 2 1

    输出#2

    23
  • 输入#3

    4 3
    1 1 1 2
    1 1 2

    输出#3

    1

说明/提示

In the first example, the strings we are interested in are [1 2 2][1\, 2\, 2] and [2 1 2][2\, 1\, 2]. The string [2 2 1][2\, 2\, 1] is lexicographically larger than the string [2 1 2 1][2\, 1\, 2\, 1], so we don't count it.

In the second example, all strings count except [4 3 2 1][4\, 3\, 2\, 1], so the answer is 4!−1=234! - 1 = 23.

In the third example, only the string [1 1 1 2][1\, 1\, 1\, 2] counts.

在第一个例子中,我们关注的字符串是 [1 2 2][1\, 2\, 2] 和 [2 1 2][2\, 1\, 2]。字符串 [2 2 1][2\, 2\, 1] 在字典序上大于字符串 [2 1 2 1][2\, 1\, 2\, 1],因此不计入。

在第二个例子中,除 [4 3 2 1][4\, 3\, 2\, 1] 外,所有字符串均计入,因此答案为 4!−1=234! - 1 = 23。

在第三个例子中,仅有字符串 [1 1 1 2][1\, 1\, 1\, 2] 计入。

输入解题思路,AI测评打分。不知道怎么写?

首页