/* The code is from koukilee*/
#include <cstdio>
using i32 = int; using i64 = long long; // std::mt19937 rd(time(0));
constexpr i64 MAXN = 1010100, mod = 1e9 + 7, INF = 992009100720100812;
//static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;
//#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
//#define putchar(x) (p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
inline void read (i64 &sum) noexcept {
i64 tf = 0; char ch = getchar(); sum = 0;
while (ch < '0' || ch > '9') tf = ch == '-' ? 1 : 0, ch = getchar();
while (ch >= '0' && ch <= '9') sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar();
(tf) && (sum =- sum);
}template <typename i64,typename ...Args>
inline void read (i64 &tmp, Args &...tmps) noexcept{read(tmp); read(tmps...);}
void printt (i64 x) noexcept {if(x >= 10) printt(x / 10); putchar(x % 10 + '0');}
inline void print (i64 x) noexcept{x < 0 ? putchar('-'), printt(-x) : printt(x);}
inline void put (i64 x) noexcept{print(x), putchar('\n');}
inline i64 min(i64 a, i64 b) noexcept{return a < b ? a : b;}
i64 n, m, k, a[MAXN], b[MAXN], cnt, dp[220][MAXN], Q[MAXN], f[MAXN], q[MAXN][5];
int main() noexcept{
read(n, m, k);
for (i64 i = 1, x; i <= m; i++)
read(x), a[x] = 1;
for (i32 i = 1; i <= k; i++)
read(b[i]);
for (i32 i = 0; i <= n; i++)
a[i] ^= a[i - 1];
for (i32 i = 0; i <= n; i++)
if (a[i])
q[cnt][0] = cnt, q[cnt][1] = i, cnt++;
for (i32 i = 0; i < cnt; i++){
for (i32 j = 0; j <= n; j++) dp[q[i][0]][j] = INF;
dp[q[i][0]][q[i][1]] = 0;
i64 l = 1, r = 1;
Q[1] = q[i][1];
while (l <= r){
i64 x = Q[l];
for (i32 j = 1; j <= k; j++){
if (x - b[j] >= 0 && dp[q[i][0]][x] + 1 < dp[q[i][0]][x - b[j]]){
dp[q[i][0]][x - b[j]] = dp[q[i][0]][x] + 1;
r++, Q[r] = x - b[j];
}
if (x + b[j] >= 0 && dp[q[i][0]][x] + 1 < dp[q[i][0]][x + b[i]]){
dp[q[i][0]][x + b[i]] = dp[q[i][0]][x] + 1;
r++, Q[r] = x + b[j];
}
l++;
}
}
}
__builtin_memset(dp, 0x3f, sizeof(dp));
f[0] = 0;
i64 mx = (1ll << cnt) - 1;
for (i32 i = 0; i <= mx; i++){
i64 x = 0;
while ((i >> x) & 1ll) x++;
for (i32 y = x + 1; y < cnt; y++)
if (!((i >> y) & 1ll))
f[i | (1ll << x) | (1ll << y)] = min(f[i | (1ll << x) | (1ll << y)], f[i] + dp[x][q[y][1]]);
}
put(f[mx] > 1e9 ? -1 : f[mx]);
//fwrite(obuf,p3-obuf,1,stdout);
return 0;
}QAQ
这是 2025年10月11日 存档的第 1 版,不是当前版本。查看当前版本