其他分享
首页 > 其他分享> > AtCoder Beginner Contest 146_E - Rem of Sum is Num

AtCoder Beginner Contest 146_E - Rem of Sum is Num

作者:互联网

预处理即可
我们要找的是 (f[i] - f[j]) % k == i - j
移项可得 f[i] - i = f[j] - ji - j <= k 的条件下
因此题目变成了,对于每个右端点,在它的左边 k - 1 个有多少个满足 f[i] - i = f[j] - j
f[i] 是前缀和数组
AC_CODE

#include <map>
#include <iostream>
#define LL long long

using namespace std;

const int N = 2e5 + 10;

LL f[N];
map<LL, int> mp;
LL ans;
int main() {
    
    int n, k; cin >> n >> k;
    for(int i = 1; i <= n; i ++ ) {
        cin >> f[i]; f[i] += f[i - 1];
    }
    for(int i = 1; i <= n; i ++ )   
        f[i] = (f[i] - i) % k;
    int idx = min(n, k - 1);
    for(int i = 0; i <= idx; i ++ ) {
        ans += mp[f[i]];
        mp[f[i]] ++;
        // cout << ans << endl;
    }
    for(int i = idx + 1; i <= n; i ++ ) {
        mp[f[i - k]] --;
        ans += mp[f[i]];
        mp[f[i]] ++;
    }
    cout << ans << endl;
    return 0;
}

标签:146,AtCoder,CODE,Beginner,int,LL,long,include,移项
来源: https://www.cnblogs.com/c972937/p/15436952.html