CF1374D(补题)

发布时间 2024-01-11 11:37:25作者: jvdyvan

思路

用map记录有多少个相同的(a[i]%k)的值,然后利用等差数列求和公式求最大值就行。
比如a = [6, 7, 5, 9, 50, 31], 且k = 3。a[i] % k --> a = [0, 1, 2, 0, 2, 1]。x要分别为2 5 才能使得a[2]和a[6]满足题目要求

ac代码

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
const i64 inf = 8e18;
typedef pair<int, int> pii;
const int N = 1e5 + 10;

void solve() {
    i64 n, k;
    cin >> n >> k;
    map<i64, i64> mp;

    for (int i = 0; i < n; i++) {
        i64 x; cin >> x;
        if (x % k == 0) continue;
        mp[x % k] ++;
    }

    i64 ans = 0;
    for (auto [x, y] : mp)
        ans = max(ans, (k - x) + (y - 1) * k + 1);

    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t --) solve();

    return 0;
}