题意:
给定一个N个数的置换a,和k个数的置换B,以及一个数M,每次可以对A进行一下操作:
1.若ai可以被m整除,则可以将ai替换为m个ai/m
2.若存在连续m个ai完全相等,ze可以将这m个数替换为一个m*ai
问a经过若干操作后是否能变为b
思路:
显然我们发现操作12是可逆的,所以这个题目可以变成:问对ab操作后是否能得到同一序列,我们只需要不断的对ab做1操作展开,最后判断展开是否相等即可,需要注意的是这里展开要合并压缩,直接展开会mle
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 1e9 + 9;
const ll INF = 1e18l;
/*
priority_queue<int> big_heep;
priority_queue<int,vector<int>,greater<int> > small_heep;
struct cmp{//if return true,it means that the number in the left was smaller than the right one
bool operator () (int a,int b){
}
};
*/
ll a1[300005], a2[300005], num1[300005], num2[300005], b1[300005], b2[300005];
ll zz(ll num,ll z) {
ll x = 1;
for (int i = 1;i <= num;i++) x *= z;
return x;
}
void work() {
ll n1, n2, z, m1, m2;
m1 = m2 = 0;
cin >> n1 >> z;
memset(num1, 0, sizeof(ll) * (n1 + 4));
memset(b1, 0, sizeof(ll) * (n1 + 4));
for (int i = 1;i <= n1;i++) {
cin >> a1[i];
ll t = a1[i];
ll num = 0;
while (t % z==0) {
num ++;
t /= z;
}
num = zz(num,z);
if (t == a1[i]) {
if (m1 == 0) {
b1[++m1] = a1[i];
num1[m1]++;
}
else {
if (t != b1[m1]) {
b1[++m1] = t;
num1[m1] ++;
}
else num1[m1] ++;
}
}
else {
if (m1 == 0) {
b1[++m1] = t;
num1[m1] += num;
}
else {
if (t != b1[m1]) {
b1[++m1] = t;
num1[m1] += num;
}
else num1[m1] += num;
}
}
}
cin >> n2;
memset(num2, 0, sizeof(ll) * (n2 + 4));
memset(b2, 0, sizeof(ll) * (n2 + 4));
for (int i = 1;i <= n2;i++) {
cin >> a2[i];
ll t = a2[i];
ll num = 0;
while (t % z == 0) {
num ++;
t /= z;
}
num = zz(num, z);
if (t == a2[i]) {
if (m2 == 0) {
b2[++m2] = a2[i];
num2[m2]++;
}
else {
if (t != b2[m2]) {
b2[++m2] = t;
num2[m2] ++;
}
else num2[m2] ++;
}
}
else {
if (m2 == 0) {
b2[++m2] = t;
num2[m2]+=num;
}
else {
if (t != b2[m2]) {
b2[++m2] = t;
num2[m2] += num;
}
else num2[m2] += num;
}
}
}
if (m1 != m2) {
cout << "NO\n";
return;
}
for (int i = 1;i <= m1;i++) {
if (b1[i] != b2[i]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin >> t;
while (t--) {
work();
}
return 0;
}