C. Fishingprince Plays With Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Fishingprince is playing with an array [a1,a2,…,an][a1,a2,…,an]. He also has a magic number mm.
He can do the following two operations on it:
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][b1,b2,…,bk]. Please determine if he can turn aa into bb using any number (possibly zero) of operations.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.
The first line of each test case contains two integers nn and mm (1≤n≤5⋅1041≤n≤5⋅104, 2≤m≤1092≤m≤109).
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
The third line of each test case contains one integer kk (1≤k≤5⋅1041≤k≤5⋅104).
The fourth line of each test case contains kk integers b1,b2,…,bkb1,b2,…,bk (1≤bi≤1091≤bi≤109).
It is guaranteed that the sum of n+kn+k over all test cases does not exceed 2⋅1052⋅105.
Output
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).
Example
input
Copy
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
output
Copy
Yes Yes No Yes No
Note
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,2,2,4,2]→[1,4,4,2].
In the second testcase of the sample, we can:
-
- #include
-
- typedef long long int ll;
- using namespace std;
-
- typedef struct
- {
- ll id,num;
- } xinxi;
-
- xinxi s[10000000],t[10000000];
-
- int main()
- {
-
- int tt;
-
- cin>>tt;
-
- while(tt--)
- {
- int n,m;
-
- scanf("%d%d",&n,&m);
-
- int len=0,len1=0;
-
-
-
- for(int i=1; i<=n; i++)
- {
- int x;
-
- scanf("%d",&x);
-
- if(x%m)
- {
- if(s[len].id==x)
- {
- s[len].num++;
-
- }
- else
- {
- len++;
-
- s[len].id=x;
-
- s[len].num=1;
- }
- }
- else
- {
-
- ll cnt=1;
-
- while(x%m==0)
- {
- cnt*=(m);
-
- x/=m;
-
- }
-
-
- if(s[len].id==x)
- {
- s[len].num+=cnt;
-
- }
- else
- {
- len++;
-
- s[len].id=x;
-
- s[len].num=cnt;
- }
-
-
- }
- }
-
- len1=len;
-
- len=0;
-
-
- scanf("%d",&n);
-
- int flag=0;
-
- for(int i=1; i<=n; i++)
- {
- int x;
-
- scanf("%d",&x);
-
- if(x%m)
- {
- if(t[len].id==x)
- {
- t[len].num++;
-
- }
- else
- {
- len++;
-
- t[len].id=x;
-
- t[len].num=1;
- }
- }
- else
- {
- ll cnt=1;
-
- while(x%m==0)
- {
- cnt*=(m);
-
- x/=m;
-
- }
-
-
- if(t[len].id==x)
- {
- t[len].num+=cnt;
-
- }
- else
- {
- len++;
-
- t[len].id=x;
-
- t[len].num=cnt;
- }
-
-
- }
- }
-
-
-
- if(len1!=len)
- {
- cout<<"No"<
- }
- else
- {
-
- flag=0;
-
- for(int i=0; i<=len; i++)
- {
- if(t[i].id!=s[i].id||t[i].num!=s[i].num)
- {
- flag=1;
-
- break;
-
- }
- }
-
- if(flag)
- {
- cout<<"No"<
- }
- else
- {
-
- cout<<"YES"<
- }
- }
-
-
- for(int i=0;i<max(len1,len);i++)
- {
-
- s[i].id=0;
- s[i].num=0;
- t[i].id=0;
- t[i].num=0;
- }
-
- }
-
- return 0;
- }
-
相关阅读:
分组查询&子查询
【课设/毕业设计】电力系统潮流计算(Matlab代码实现)
visionOS空间计算实战开发教程Day 4 初识ImmersiveSpace
通义千问本地部署报错
算法、语言混编、分布式锁与分布式ID、IO模型
HTML5和CSS3四属性总结一
尚硅谷Flink(二)DStream API
SimaPro生命周期评估建模与碳足迹分析流程
TRC丨艾美捷TRC单羟基舒更葡糖钠
【Gradle-13】SNAPSHOT版本检查
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126141300