Submission #68556954


Source Code Expand

/*
    Template Version: 1.0.0 - 20220620
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define TRAV(x, a) for (auto &x : a)

using namespace std;
using ll = long long; using ld = double; 
using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<ld, ld>;
using cd = complex<ld>; using vcd = vector<cd>;

using vi = vector<int>; using vl = vector<ll>;
using vd = vector<ld>; using vs = vector<string>;
using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; // vector<pair>

template<class T> using min_pq = priority_queue<T, vector<T>, greater<T> >;
template<class T> inline int ckmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; }
template<class T> inline int ckmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sort(ALL(v)); v.erase(unique(ALL(v)), end(v)); }

constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x)) 
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }

ll ceilDiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
ll floorDiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
void setPrec(int x) { cout << fixed << setprecision(x); }

// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string) s; }
string ts(string s) { return s; }
string ts(bool b) { return (b ? "true" : "false"); }

template<class T> using V = vector<T>;
template<class T> string ts(complex<T> c);
string ts(V<bool> v);
template<size_t sz> string ts(bitset<sz> b);
template<class T> string ts(T v);
template<class T, class U> string ts(pair<T,U> p);
template<class ...U> string ts(tuple<U...> u);

template<class T> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); }
string ts(V<bool> v) {string res = "{"; FOR(i,0,SZ(v)-1) res += char('0'+v[i]); res += "}"; return res; }
template<size_t sz> string ts(bitset<sz> b) { string res = ""; FOR(i,0,SZ(b)-1) res += char('0'+b[i]); return res; }
template<class T> string ts(T v) { // containers with begin(), end()
    bool fst = 1; string res = "";
    for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); }
    return res;
}
template<class T, class U> string ts(pair<T,U> p) { return "(" + ts(p.FI) + ", " + ts(p.SE) + ")"; }
template<size_t i, class T> string print_tuple_utils(const T& tup) { if constexpr(i == tuple_size<T>::value) return ")"; else return (i ? ", " : "(") + ts(get<i>(tup)) + print_tuple_utils<i + 1, T>(tup); }
template<class ...U> string ts(tuple<U...> u) { return print_tuple_utils<0, tuple<U...>>(u); }

// OUTPUT
template<class T> void pr(T x) { cout << ts(x); }
template<class T, class ...U> void pr(const T& t, const U&... u) { pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
template<class T, class ...U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
template<class T, class ...U> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); }

#ifdef LOCAL_DEBUG
#define CONCAT(x, y) x##y
#define with_level setw(__db_level * 2) << setfill(' ') << "" << setw(0)
#define dbg(...) cerr << with_level << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << setw(__db_level * 2) << setfill(' ') << "" << setw(0) << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#define db_block() debug_block CONCAT(dbbl, __LINE__)
int __db_level = 0;
struct debug_block {
    debug_block() { cerr << with_level << "{" << endl; ++__db_level; }
    ~debug_block() { --__db_level; cerr << with_level << "}" << endl; }
};
#else
#define dbg(...) 0
#define chk(...) 0
#define db_block() 0
#endif

const ld PI = acos(-1.0);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const ld EPS = 1e-9;
const ll MODBASE = 1000000007LL;
const int INF = 0x3f3f3f3f;

const int MAXN = 510;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, m, l, a[MAXN], b[MAXN];
int f[MAXN][MAXN];
int dp[MAXN][MAXN];

int solve() {
    FOR(i,1,l) {
        vi C;
        for (int j = i; j <= n; j += l) {
            C.push_back(a[j]);
        }

        FOR(j,0,m-1) {
            int w = 0;
            for (int x : C) {
                if (x <= j) w += j - x;
                else w += m - x + j;
            }
            f[i][j] = w;
            // dbg(i, j, f[i][j]);
        }
    }

    // dp[i][j] = min number of additions needed to make sum(a[1..i]) % m = j
    FOR(i,0,l) FOR(j,0,m-1) dp[i][j] = INF;
    dp[0][0] = 0;
    FOR(i,0,l-1) {
        FOR(j,0,m-1) {
            if (dp[i][j] == INF) continue;
            FOR(p,0,m-1) {
                int newJ = (j + p) % m;
                ckmin(dp[i+1][newJ], dp[i][j] + f[i+1][p]);
            }
        }
    }

    return dp[l][0];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> m >> l;
    FOR(i,1,n) cin >> a[i];
    cout << solve();
    return 0;
}

Submission Info

Submission Time
Task E - Subarray Sum Divisibility
User farmerboy
Language C++ 20 (gcc 12.2)
Score 475
Code Size 5791 Byte
Status AC
Exec Time 218 ms
Memory 5476 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 2
AC × 37
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 3536 KiB
sample01.txt AC 1 ms 3488 KiB
testcase00.txt AC 1 ms 3388 KiB
testcase01.txt AC 1 ms 3528 KiB
testcase02.txt AC 1 ms 3552 KiB
testcase03.txt AC 1 ms 3440 KiB
testcase04.txt AC 109 ms 4496 KiB
testcase05.txt AC 207 ms 5476 KiB
testcase06.txt AC 1 ms 3500 KiB
testcase07.txt AC 104 ms 4352 KiB
testcase08.txt AC 211 ms 5412 KiB
testcase09.txt AC 1 ms 3504 KiB
testcase10.txt AC 12 ms 3512 KiB
testcase11.txt AC 218 ms 5328 KiB
testcase12.txt AC 1 ms 3436 KiB
testcase13.txt AC 2 ms 3488 KiB
testcase14.txt AC 1 ms 3436 KiB
testcase15.txt AC 1 ms 3464 KiB
testcase16.txt AC 1 ms 3452 KiB
testcase17.txt AC 19 ms 3836 KiB
testcase18.txt AC 1 ms 3576 KiB
testcase19.txt AC 3 ms 4872 KiB
testcase20.txt AC 1 ms 3520 KiB
testcase21.txt AC 1 ms 3488 KiB
testcase22.txt AC 2 ms 3552 KiB
testcase23.txt AC 7 ms 3552 KiB
testcase24.txt AC 19 ms 3660 KiB
testcase25.txt AC 156 ms 4980 KiB
testcase26.txt AC 183 ms 5400 KiB
testcase27.txt AC 176 ms 5452 KiB
testcase28.txt AC 1 ms 3544 KiB
testcase29.txt AC 3 ms 3520 KiB
testcase30.txt AC 7 ms 3540 KiB
testcase31.txt AC 25 ms 3716 KiB
testcase32.txt AC 197 ms 5272 KiB
testcase33.txt AC 212 ms 5428 KiB
testcase34.txt AC 218 ms 5388 KiB