题意
给了你两个数组a,b,a的长度是n,b的长度是n,然后告诉了你一个m。你先在可以执行以下操作:
1.你可以将数组中的a[i]分解成m个(如果能够整数)。如果m = 2,[2, 3] => [1, 1, 3]。
2.如果一段的连续的区间内,你可以将它变成x,如果a[i]*m = x。[3, 2, 2, 3] => [3, 4,3]。
为你是否能够通过以上的操作,让a数组和数组对应的位置的值是一样的。
思路
现在可以执行两种操作,那么我们就执行一种,只执行第一种操作,就是将两个数组里面的可以分解的都分解了,然后最后再来判断一下对应的位置是否相等。 那么我们可以这样:在一段区间内,如果这一段区间内所有值分解都最终结果都是一样的,那么我们用一个结构体来装,1)这个最终分解的值,2)分解的值的和。看代码:
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N = 5e4 + 10;
void solve()
{
int n, m, k;
cin >> n >> m;
vector<pair<int, int>> a, b;
for (int i = 1; i <= n; i ++)
{
int x; cin >> x;
int cnt = 1;
while (x % m == 0) { cnt *= m; x /= m; }
if (a.empty() || a.back().x != x) a.push_back({x, cnt});
else a.back().y += cnt;
}
cin >> k;
for (int i = 1; i <= k; i ++)
{
int x; cin >> x;
int cnt = 1;
while (x % m == 0) { cnt *= m; x /= m; }
if (b.empty() || b.back().x != x) b.push_back({x, cnt});
else b.back().y += cnt;
}
cout << (a == b ? "Yes" : "No") << '\n';
}
signed main()
{
std::ios::sync_with_stdio(false);
int t; cin >> t;
while (t --) solve();
return 0;
}