跳到正文

题解

题解:P16201 【MX-S13-T1】旋转工艺

asd890123约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年4月6日
收录
2026年4月6日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
2
题目
P16201

版本

  1. v2当前
  2. v1

首先,既然是循环序列,我们先把原数组复制一份加到后面,然后计算前缀和,问题就变为了对于每一个满足 1≤l≤n1\le l\le n 的 ll,是否存在满足 l≤r≤l+n−1l\le r\le l + n-1 的 rr 同时也满足 sumr=suml−1+ksum_r = sum_{l - 1} + k,直接枚举是 O(n2)O(n^2) 无法通过,所以我们用一个 map,每次更新每一个数在 [l,l+n)[l,l + n) 中的出现频率,时间复杂度 O(n)O(n),可以通过。

C++
#include<bits/stdc++.h>
typedef long long ll;
const int N = 1e6 + 5;
int a[N + N];
ll sum[N + N];
std::unordered_map<ll,int> cnt;
int main(){
    std::cin.tie(0)->sync_with_stdio(0);
    int c,t;std::cin >> c >> t;
    while (t--){
        int n;ll k;std::cin >> n >> k;
        for (int i = 1;i <= n;i++) std::cin >> a[i],a[i + n] = a[i];
        sum[0] = 0;
        for (int i = 1;i <= n + n;i++) sum[i] = sum[i - 1] + a[i];
        int ans = 0;
        cnt.clear();
        for (int i = 1;i <= n;i++) ++cnt[sum[i]];
        for (int l = 1;l <= n;l++){
            if (cnt[sum[l - 1] + k]) ++ans;
            --cnt[sum[l]];
            ++cnt[sum[l + n]];
        }
        std::cout << ans << '\n';
    }
    return 0;
}
原文