CF1696C.Fishingprince Plays With Array

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Fishingprince is playing with an array [a1,a2,…,an][a_1,a_2,\dots,a_n]. He also has a magic number mm.

He can do the following two operations on it:

  • Select 1≤i≤n1\le i\le n such that aia_i is divisible by mm (that is, there exists an integer tt such that m⋅t=aim \cdot t = a_i). Replace aia_i with mm copies of aim\frac{a_i}{m}. The order of the other elements doesn't change. For example, when m=2m=2 and a=[2,3]a=[2,3] and i=1i=1, aa changes into [1,1,3][1,1,3].
  • Select 1≤i≤n−m+11\le i\le n-m+1 such that ai=ai+1=⋯=ai+m−1a_i=a_{i+1}=\dots=a_{i+m-1}. Replace these mm elements with a single m⋅aim \cdot a_i. The order of the other elements doesn't change. For example, when m=2m=2 and a=[3,2,2,3]a=[3,2,2,3] and i=2i=2, aa changes into [3,4,3][3,4,3].

Note that the array length might change during the process. The value of nn above is defined as the current length of the array (might differ from the nn in the input).

Fishingprince has another array [b1,b2,…,bk][b_1,b_2,\dots,b_k]. Please determine if he can turn aa into bb using any number (possibly zero) of operations.

Fishingprince 正在操作一个数组 [a1,a2,…,an][a_1,a_2,\dots,a_n],他还有一个魔法数 mm。

他可以对数组执行以下两种操作:

  • 选择满足 1≤i≤n1\le i\le n 且 aia_i 能被 mm 整除(即存在整数 tt 使得 m⋅t=aim \cdot t = a_i)的下标 ii,将 aia_i 替换为 mm 个 aim\frac{a_i}{m}。其余元素的相对顺序保持不变。例如,当 m=2m=2、a=[2,3]a=[2,3] 且 i=1i=1 时,aa 变为 [1,1,3][1,1,3]。
  • 选择满足 1≤i≤n−m+11\le i\le n-m+1 且 ai=ai+1=⋯=ai+m−1a_i=a_{i+1}=\dots=a_{i+m-1} 的下标 ii,将这 mm 个相等的元素替换为单个数 m⋅aim \cdot a_i。其余元素的相对顺序保持不变。例如,当 m=2m=2、a=[3,2,2,3]a=[3,2,2,3] 且 i=2i=2 时,aa 变为 [3,4,3][3,4,3]。

注意:在操作过程中,数组长度可能发生变化。上述描述中的 nn 表示当前数组的长度(可能与输入中的 nn 不同)。

Fishingprince 还有另一个数组 [b1,b2,…,bk][b_1,b_2,\dots,b_k]。请判断他能否通过任意次数(包括零次)的操作将数组 aa 变为数组 bb。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤5⋅1041\le n\le 5\cdot 10^4, 2≤m≤1092\le m\le 10^9).

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤1091\le a_i\le 10^9).

The third line of each test case contains one integer kk (1≤k≤5⋅1041\le k\le 5\cdot 10^4).

The fourth line of each test case contains kk integers b1,b2,…,bkb_1,b_2,\ldots,b_k (1≤bi≤1091\le b_i\le 10^9).

It is guaranteed that the sum of n+kn+k over all test cases does not exceed 2⋅1052\cdot 10^5.

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

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤5⋅1041\le n\le 5\cdot 10^4,2≤m≤1092\le m\le 10^9)。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤1091\le a_i\le 10^9)。

每个测试用例的第三行包含一个整数 kk(1≤k≤5⋅1041\le k\le 5\cdot 10^4)。

每个测试用例的第四行包含 kk 个整数 b1,b2,…,bkb_1,b_2,\ldots,b_k(1≤bi≤1091\le b_i\le 10^9)。

保证所有测试用例中 n+kn+k 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each testcase, print Yes if it is possible to turn aa into bb, and No otherwise. You can print each letter in any case (upper or lower).

对于每个测试用例,如果可以将 aa 变为 bb,则输出 Yes;否则输出 No。字母大小写均可(即 Yes、YES、yes 等均有效)。

输入输出样例

  • 输入#1

    5
    5 2
    1 2 2 4 2
    4
    1 4 4 2
    6 2
    1 2 2 8 2 2
    2
    1 16
    8 3
    3 3 3 3 3 3 3 3
    4
    6 6 6 6
    8 3
    3 9 6 3 12 12 36 12
    16
    9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4
    8 3
    3 9 6 3 12 12 36 12
    7
    12 2 4 3 4 12 56

    输出#1

    Yes
    Yes
    No
    Yes
    No

说明/提示

In the first test case of the sample, we can do the second operation with i=2i=2: [1,2,2,4,2]→[1,4,4,2][1,\color{red}{2,2},4,2]\to [1,\color{red}{4},4,2].

In the second testcase of the sample, we can:

  • do the second operation with i=2i=2: [1,2,2,8,2,2]→[1,4,8,2,2][1,\color{red}{2,2},8,2,2]\to [1,\color{red}{4},8,2,2].
  • do the second operation with i=4i=4: [1,4,8,2,2]→[1,4,8,4][1,4,8,\color{red}{2,2}]\to [1,4,8,\color{red}{4}].
  • do the first operation with i=3i=3: [1,4,8,4]→[1,4,4,4,4][1,4,\color{red}{8},4]\to [1,4,\color{red}{4,4},4].
  • do the second operation with i=2i=2: [1,4,4,4,4]→[1,8,4,4][1,\color{red}{4,4},4,4]\to [1,\color{red}{8},4,4].
  • do the second operation with i=3i=3: [1,8,4,4]→[1,8,8][1,8,\color{red}{4,4}]\to [1,8,\color{red}{8}].
  • do the second operation with i=2i=2: [1,8,8]→[1,16][1,\color{red}{8,8}]\to [1,\color{red}{16}].

在样例的第一个测试用例中,我们可以对 i=2i=2 执行第二种操作:[1,2,2,4,2]→[1,4,4,2][1,\color{red}{2,2},4,2]\to [1,\color{red}{4},4,2]。

在样例的第二个测试用例中,我们可以:

  • 对 i=2i=2 执行第二种操作:[1,2,2,8,2,2]→[1,4,8,2,2][1,\color{red}{2,2},8,2,2]\to [1,\color{red}{4},8,2,2]。
  • 对 i=4i=4 执行第二种操作:[1,4,8,2,2]→[1,4,8,4][1,4,8,\color{red}{2,2}]\to [1,4,8,\color{red}{4}]。
  • 对 i=3i=3 执行第一种操作:[1,4,8,4]→[1,4,4,4,4][1,4,\color{red}{8},4]\to [1,4,\color{red}{4,4},4]。
  • 对 i=2i=2 执行第二种操作:[1,4,4,4,4]→[1,8,4,4][1,\color{red}{4,4},4,4]\to [1,\color{red}{8},4,4]。
  • 对 i=3i=3 执行第二种操作:[1,8,4,4]→[1,8,8][1,8,\color{red}{4,4}]\to [1,8,\color{red}{8}]。
  • 对 i=2i=2 执行第二种操作:[1,8,8]→[1,16][1,\color{red}{8,8}]\to [1,\color{red}{16}]。

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

首页