CF1696C.Fishingprince Plays With Array
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Fishingprince is playing with an array [a1,a2,…,an]. He also has a magic number m.
He can do the following two operations on it:
- Select 1≤i≤n such that ai is divisible by m (that is, there exists an integer t such that m⋅t=ai). Replace ai with m copies of mai. The order of the other elements doesn't change. For example, when m=2 and a=[2,3] and i=1, a changes into [1,1,3].
- Select 1≤i≤n−m+1 such that ai=ai+1=⋯=ai+m−1. Replace these m elements with a single m⋅ai. The order of the other elements doesn't change. For example, when m=2 and a=[3,2,2,3] and i=2, a changes into [3,4,3].
Note that the array length might change during the process. The value of n above is defined as the current length of the array (might differ from the n in the input).
Fishingprince has another array [b1,b2,…,bk]. Please determine if he can turn a into b using any number (possibly zero) of operations.
Fishingprince 正在操作一个数组 [a1,a2,…,an],他还有一个魔法数 m。
他可以对数组执行以下两种操作:
- 选择满足 1≤i≤n 且 ai 能被 m 整除(即存在整数 t 使得 m⋅t=ai)的下标 i,将 ai 替换为 m 个 mai。其余元素的相对顺序保持不变。例如,当 m=2、a=[2,3] 且 i=1 时,a 变为 [1,1,3]。
- 选择满足 1≤i≤n−m+1 且 ai=ai+1=⋯=ai+m−1 的下标 i,将这 m 个相等的元素替换为单个数 m⋅ai。其余元素的相对顺序保持不变。例如,当 m=2、a=[3,2,2,3] 且 i=2 时,a 变为 [3,4,3]。
注意:在操作过程中,数组长度可能发生变化。上述描述中的 n 表示当前数组的长度(可能与输入中的 n 不同)。
Fishingprince 还有另一个数组 [b1,b2,…,bk]。请判断他能否通过任意次数(包括零次)的操作将数组 a 变为数组 b。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n≤5⋅104, 2≤m≤109).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
The third line of each test case contains one integer k (1≤k≤5⋅104).
The fourth line of each test case contains k integers b1,b2,…,bk (1≤bi≤109).
It is guaranteed that the sum of n+k over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n≤5⋅104,2≤m≤109)。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)。
每个测试用例的第三行包含一个整数 k(1≤k≤5⋅104)。
每个测试用例的第四行包含 k 个整数 b1,b2,…,bk(1≤bi≤109)。
保证所有测试用例中 n+k 的总和不超过 2⋅105。
输出格式
For each testcase, print Yes if it is possible to turn a into b, and No otherwise. You can print each letter in any case (upper or lower).
对于每个测试用例,如果可以将 a 变为 b,则输出 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=2: [1,2,2,4,2]→[1,4,4,2].
In the second testcase of the sample, we can:
- do the second operation with i=2: [1,2,2,8,2,2]→[1,4,8,2,2].
- do the second operation with i=4: [1,4,8,2,2]→[1,4,8,4].
- do the first operation with i=3: [1,4,8,4]→[1,4,4,4,4].
- do the second operation with i=2: [1,4,4,4,4]→[1,8,4,4].
- do the second operation with i=3: [1,8,4,4]→[1,8,8].
- do the second operation with i=2: [1,8,8]→[1,16].
在样例的第一个测试用例中,我们可以对 i=2 执行第二种操作:[1,2,2,4,2]→[1,4,4,2]。
在样例的第二个测试用例中,我们可以:
- 对 i=2 执行第二种操作:[1,2,2,8,2,2]→[1,4,8,2,2]。
- 对 i=4 执行第二种操作:[1,4,8,2,2]→[1,4,8,4]。
- 对 i=3 执行第一种操作:[1,4,8,4]→[1,4,4,4,4]。
- 对 i=2 执行第二种操作:[1,4,4,4,4]→[1,8,4,4]。
- 对 i=3 执行第二种操作:[1,8,4,4]→[1,8,8]。
- 对 i=2 执行第二种操作:[1,8,8]→[1,16]。
输入解题思路,AI测评打分。不知道怎么写?