题意就是给一个数m,有两种操作,看看能否从序列a到序列b
操作1:如果ai能整除m,那一个数ai变成m个ai/m(其他数字及其位置保持不变)
操作2:如果有m个数字相同,可以把这m个变成ai*m(m个变成这一个)
这题的思路很好想,直接划成一样的不现实,直接实现操作2不现实,而且小的数是可以合并成大的数的,所以在ab操作的时候,把数都分解,分解到最小(相当于把要变成的数字固定住了,不然可能会存在ab大小不一的情况,很麻烦,直接化成最小)
但是可以看一眼数据范围很大,我一个队友直接化然后溢出了(小声嘀咕~不是说他QAQ),这里的优化其实我也是没想到的,用区间来优化,就是每一段的数放到一个区间里面,一个数字占多少,就是把这一段数缩小成一个,个数另外算,然后在区间的基础上比较a和b,如果相等就yes,不行就no
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PAII;
int main(){
int T;
cin>>T;
while(T--)
{
vector<PAII> a,b;
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
ll x;
cin>>x;
ll cnt=1;
while(x%m==0)
{
cnt*=m;
x/=m;
}
if(a.empty()||a.back().first!=x) a.push_back({x,cnt});//添加一个区间
else a.back().second+=cnt;
}
int k;
cin>>k;
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
ll cnt=1;
while(x%m==0)
{
cnt*=m;
x/=m;
}
if(b.empty()||b.back().first!=x) b.push_back({x,cnt});//添加一个区间
else b.back().second+=cnt;
}
if(a==b) cout<<"Yes"<<"\n";
else cout<<"No"<<"\n";
}
return 0;
}
多积累一点优化,像这种题,如果一段都是一样的,还存它干啥,浪费空间呐,直接存二维的。一维存数字,二维存个数,然后,两者一起比较