ABC-419-E - Subarray Sum Divisibility(len连续段和都是M倍数的所需最少操作数,操作是让一个元素增加1) · znzryb
## 思路讲解
$(A[1]+\dots+A[len]) \bmod M = 0$
$(A[2]+\dots+A[len+1]) \bmod M = 0$
$(-A[1]+A[len+1])\bmod M=0$
$Sum_{i+L}≡Sum_i(\bmod M)(∀i=0,…,N−L).$
[https://chatgpt.com/c/68a1e9d2-d4f4-832a-bc89-13ae40f158c5](https://chatgpt.com/c/68a1e9d2-d4f4-832a-bc89-13ae40f158c5)
br 是指的差值的模(模M)。具体来讲,就是 $(-A[1]+A[len+1])\bmod M=0$ ,$(-A[0]+A[len])\bmod M=0$ 这种位置很多,他们成周期性出现,即是:
$b_1=A[1]\bmod M=A[len+1]\bmod M=A[2len+1]\bmod M \cdots$
那么,不难发现,除了要满足传递性,还要满足初始条件,即:
$b_1+b_2+⋯+b_{len}≡0 \pmod M$
然后可以定义一个dp数组,$dp[i][j]$ 表示已经选了前 i 个数字,且 $sum\equiv j\pmod M$ 所需的代价为几($sum=b_1+b_2+\cdots+b_i$)?
为了快速计算这个dp,可以使用一个辅助数组$g[i][j]$ 是指$b[i]$ 到达$b[i]\equiv j \pmod M$ 所需要的总代价。朴素计算需要$O(LM \frac{N}{L}) \Rightarrow O(MN)$ ( 好像还可以?)
## AC代码
[https://atcoder.jp/contests/abc419/submissions/68614283](https://atcoder.jp/contests/abc419/submissions/68614283)
::: 源代码
```cpp
// by Gospel_rock
#include <bits/stdc++.h>
#include <numeric>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define lson(var) (var<<1)
#define rson(var) (var<<1|1)
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT,L;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
// while(lT--)
__();
return 0;
}
/*
a1+...+al
a2+...+al+1 al+1 - a1 % M ==0
*/
inline ll MOD(const ll &a,const ll &b){
ll res=a%b;
if(res<0) res+=b;
return res;
}
inline void __(){
cin>>N>>M>>L;
vll A(N+5);
FOR(i,1,N){
cin>>A[i];
}
// 为了快速计算这个dp,可以使用一个辅助数组$g[i][j]$ 是指$b[i]$ 到达$b[i]\equiv j \pmod M$ 所需要的总代价。朴素计算需要$O(LM \frac{N}{L}) \Rightarrow O(MN)$ ( 好像还可以?)
vector<vll> g(L+5,vll(M+5));
auto cal=[&](ll x,ll obj){
// if(MOD(x,M)==obj) return 0ll;
if(x/M*M+obj>=x){
return x/M*M+obj-x;
}else{
return (x+M-1)/M*M+obj-x;
}
};
FOR(i,1,L){
FOR(j,0,M-1){
ll res=0;
for(int p=i;p<=N;p+=L){
res+=cal(A[p],j);
}
g[i][j]=res;
}
}
vector<vll> dp(L+5,vll(M+5,INF));
dp[0][0]=0;
FOR(i,1,L){
FOR(sum,0,M-1){
FOR(j,0,M-1){
ll res=MOD(sum-j,M);
dp[i][sum]=min(dp[i-1][res]+g[i][j],dp[i][sum]);
}
}
}
#ifdef LOCAL
_print(dp);
_print(g);
#endif
ll ans=dp[L][0];
cout<<ans;
}
/*
AC
https://atcoder.jp/contests/abc419/submissions/68614283
*/
// vll Sum(N+5);
// partial_sum(all(A),Sum.begin());
// ll ans=0;
// auto cal=[&](ll x){
// x=abs(x);
// ll res=x-x/M*M;
// res=min(res,(x+M-1)/M*M-x);
// return res;
// };
// FOR(i,1,N-L){
// ll diff=A[L+i]-A[i];
// // ll lans=MOD(diff,M);
// // diff=diff%M;
// // diff=abs(diff);
// ll lans=cal(diff);
// #ifdef LOCAL
// debug(i);debug(diff);debug(lans);
// #endif
// ans+=cal(diff);
// }
// cout<<ans<<"\n";
```
:::
## 心路历程(WA,TLE,MLE……)