Submission #68573981
Source Code Expand
#include <bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = n-1; i >=0; i--) #define repsg(i, s, g) for (int i = s; i < (int)(g); i++) using ll = long long; using P = pair<int,int>; using Graph = vector<vector<int>>; using mint = modint1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M, L; cin >> N >> M >> L; vector<int> A(N); rep(i, N) cin >> A[i]; // 入る値と抜ける値の余り(Mod M)が等しくないといけない // r0 : Ai ≡ Ai+L ≡ Ai+2L .. // r1 : Ai+1 ≡ Ai+1+L ≡ Ai+1+2L .. // .. // 位置を L で割った余りごとのクラス r に分割 // そのクラス内の「値 ≡ d (mod M)」の個数 freq[r][d] を数える vector<vector<int>> freq(L, vector<int>(M, 0)); repsg(k, 1, N+1){ int r = (k-1)%L; int d = A[k-1]%M; freq[r][d]++; } // dp[m] : これまで処理したクラスの目標値和 ≡ m (mod M) にする最小増加回数 ll INF = (1LL<<62); vector<ll> dp(M, INF); dp[0] = 0; //// 各クラス r について最適な「そろえ先 x (mod M)」を選ぶ //// rep(r, L){ int n = 0; rep(d, M) n += freq[r][d]; if (n == 0) continue; // sum = Σ d * freq[r][d] ll sum = 0; rep(d, M) sum += d * freq[r][d]; // cle[x] = Σ {d=0..x} freq[r][d](累積個数) vector<int> cle(M, 0); int s = 0; rep(d, M) { s += freq[r][d]; cle[d] = s; } // クラス r の全要素を x(mod M) へ増やして揃えるためのコスト(全体) // H[x] = cost_r(x) = Σ d freq[d] * ((x-d) mod M) // 全要素数 n // sum = Σ d * freq[r][d] // H[x] = Σ freq[d]*(x-d) (d≤x) + Σ freq[d]*(x-d+M) (d>x) // H[x] = n*x - sum + M*(n-cle[x]) vector<ll> H(M, 0); rep(x, M) H[x] = 1LL*n*x - sum + 1LL*M*(n-cle[x]); //// クラスごとのコスト H[x] を組み合わせて、全体の剰余制約を満たすように選んでいく // dp 遷移 / dp[0]が「合計 ≡ 0 (mod M)」になるように揃えたときの最小コスト // これまでの和 ≡ m に、次クラスの目標 x を足して 新しい和 (m+x) mod M のコストを更新 vector<ll> ndp(M, INF); rep(m, M) if (dp[m] < INF) { rep(x, M) { int idx = (m+x) % M; ll cand = dp[m] + H[x]; if (cand < ndp[idx]) ndp[idx] = cand; } } dp.swap(ndp); } ll ans = dp[0]; cout << ans << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Subarray Sum Divisibility |
User | tkar821 |
Language | C++ 23 (gcc 12.2) |
Score | 475 |
Code Size | 2653 Byte |
Status | AC |
Exec Time | 219 ms |
Memory | 4200 KiB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 475 / 475 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample00.txt, sample01.txt |
All | sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample00.txt | AC | 1 ms | 3456 KiB |
sample01.txt | AC | 1 ms | 3604 KiB |
testcase00.txt | AC | 1 ms | 3592 KiB |
testcase01.txt | AC | 2 ms | 3512 KiB |
testcase02.txt | AC | 1 ms | 3480 KiB |
testcase03.txt | AC | 1 ms | 3552 KiB |
testcase04.txt | AC | 109 ms | 3596 KiB |
testcase05.txt | AC | 207 ms | 4144 KiB |
testcase06.txt | AC | 1 ms | 3492 KiB |
testcase07.txt | AC | 104 ms | 3588 KiB |
testcase08.txt | AC | 212 ms | 4172 KiB |
testcase09.txt | AC | 1 ms | 3564 KiB |
testcase10.txt | AC | 12 ms | 3520 KiB |
testcase11.txt | AC | 219 ms | 4160 KiB |
testcase12.txt | AC | 1 ms | 3560 KiB |
testcase13.txt | AC | 2 ms | 3496 KiB |
testcase14.txt | AC | 1 ms | 3400 KiB |
testcase15.txt | AC | 1 ms | 3404 KiB |
testcase16.txt | AC | 1 ms | 3604 KiB |
testcase17.txt | AC | 18 ms | 3592 KiB |
testcase18.txt | AC | 1 ms | 3516 KiB |
testcase19.txt | AC | 3 ms | 3636 KiB |
testcase20.txt | AC | 1 ms | 3464 KiB |
testcase21.txt | AC | 1 ms | 3560 KiB |
testcase22.txt | AC | 2 ms | 3556 KiB |
testcase23.txt | AC | 8 ms | 3504 KiB |
testcase24.txt | AC | 19 ms | 3708 KiB |
testcase25.txt | AC | 156 ms | 3844 KiB |
testcase26.txt | AC | 184 ms | 4084 KiB |
testcase27.txt | AC | 177 ms | 4140 KiB |
testcase28.txt | AC | 1 ms | 3448 KiB |
testcase29.txt | AC | 3 ms | 3632 KiB |
testcase30.txt | AC | 7 ms | 3632 KiB |
testcase31.txt | AC | 26 ms | 3724 KiB |
testcase32.txt | AC | 197 ms | 4108 KiB |
testcase33.txt | AC | 212 ms | 4184 KiB |
testcase34.txt | AC | 219 ms | 4200 KiB |