Codeforces Global Round 21 C. Fishingprince Plays With Array

链接

题意

给了你两个数组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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值