CF2174C1.Beautiful Patterns (Easy Version)

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, n≤2⋅103n \le 2 \cdot 10^3. You can hack only if you solved all versions of this problem.

Upon entering the ancient palace "Palindrome-Palace", you noticed that there are peculiar patterns on its walls. The pattern is a mosaic of size 1×n1 \times n made of pebbles, each painted in one of mm different colors.

The correctness of an arbitrary mosaic ss is defined as the number of non-empty subsegments of ss that are palindromes. The beauty of the mosaic is defined as the square of its correctness. For example, for the mosaic rgrb, there are five palindromic subsegments: r, g, r, b, and rgr. Therefore, its correctness is 55, and its beauty is 2525.

While wandering through this palace, you wondered: what is the expected value of the beauty of the mosaic if the color of each of the nn pebbles is chosen uniformly and independently of the colors of the other pebbles? Print the answer modulo prime pp.

这是该问题的简单版本。两个版本的区别在于,在此版本中,n≤2⋅103n \le 2 \cdot 10^3。仅当您解决了该问题的所有版本时,才可进行 hack。

当你进入古老宫殿“回文宫”(Palindrome-Palace)时,你注意到其墙壁上有着奇特的图案。该图案是一条尺寸为 1×n1 \times n 的马赛克,由鹅卵石拼成,每颗鹅卵石被涂成 mm 种不同颜色之一。

任意马赛克 ss 的正确性定义为 ss 中非空回文子段的个数;其美观度定义为正确性的平方。例如,对于马赛克 rgrb,共有五个回文子段:r、g、r、b 和 rgr。因此其正确性为 55,美观度为 2525。

当你在这座宫殿中漫游时,你不禁思考:若 nn 颗鹅卵石的颜色各自独立、均匀地从 mm 种颜色中随机选取,则该马赛克的美观度的期望值是多少?请将答案对质数 pp 取模后输出。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

The only line of each test case contains three integers nn, mm, and pp (1≤n≤2⋅1031 \leq n \leq 2 \cdot 10^3; 1≤m≤1071 \leq m \leq 10^7; m<p<109m \lt p \lt 10^9) representing the length of the mosaic, the number of different colors of pebbles, and the modulus for which the answer needs to be computed.

It is guaranteed that pp is a prime number. It is also guaranteed that the sum of nn across all test cases does not exceed 2⋅1032 \cdot 10^3.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例仅有一行,包含三个整数 nn、mm 和 pp(1≤n≤2⋅1031 \leq n \leq 2 \cdot 10^3;1≤m≤1071 \leq m \leq 10^7;m<p<109m \lt p \lt 10^9),分别表示马赛克的长度、不同颜色鹅卵石的数量,以及答案需对其取模的模数。

保证 pp 是一个质数。同时保证所有测试用例中 nn 的总和不超过 2⋅1032 \cdot 10^3。

输出格式

For each test case, output the expected beauty of the mosaic modulo pp.

Formally, let x=px = p. It can be shown that the exact answer can be expressed as an irreducible fraction yz\frac{y}{z}, where yy and zz are integers and z≢0(modx)z \not \equiv 0 \pmod{x}. Output the integer equal to y⋅z−1 mod xy \cdot z^{-1} \bmod x. In other words, output such an integer tt that 0≤t<x0 \le t \lt x and t⋅z≡y(modx)t \cdot z \equiv y \pmod{x}.

对每个测试用例,输出马赛克的期望美观度对 pp 取模的结果。

形式化地,令 x=px = p。可以证明,精确答案可表示为既约分数 yz\frac{y}{z},其中 yy 和 zz 为整数,且 z≢0(modx)z \not \equiv 0 \pmod{x}。请输出满足 y⋅z−1 mod xy \cdot z^{-1} \bmod x 的整数。换言之,请输出满足 0≤t<x0 \le t < x 且 t⋅z≡y(modx)t \cdot z \equiv y \pmod{x} 的整数 tt。

输入输出样例

  • 输入#1

    3
    2 2 101
    5 1 999999937
    100 23190 3214373

    输出#1

    57
    225
    2347147

说明/提示

In the first test case, there are a total of four mosaics of length 22 if only two different colors of pebbles can be used to construct them, with two of them having two palindromic subsegments, and the other two having three each. Thus, the expected value of the beauty of the mosaic is (224+224+324+324)=13⋅2−1 mod 101=57\left(\frac{2^2}{4} + \frac{2^2}{4} + \frac{3^2}{4} + \frac{3^2}{4}\right) = 13 \cdot 2^{-1} \bmod 101 = 57.

In the second test case, all subsegments of the mosaic of length 5 will be palindromes.

在第一个测试用例中,若仅使用两种不同颜色的卵石来构造,长度为 22 的马赛克共有 44 种,其中两种各有 22 个回文子段,另外两种各有 33 个回文子段。因此,该马赛克美观度的期望值为 (224+224+324+324)=13⋅2−1 mod 101=57\left(\frac{2^2}{4} + \frac{2^2}{4} + \frac{3^2}{4} + \frac{3^2}{4}\right) = 13 \cdot 2^{-1} \bmod 101 = 57。

在第二个测试用例中,长度为 55 的马赛克的所有子段均为回文。

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

首页