[Codeforces Round #677 (Div. 3)] F. Zero Remainder Sum (DP)
题目链接:https://codeforces.com/problemset/problem/1433/F
题面:
题意:
给你一个(n imes m) 的矩阵,每一行你可以最多选择(leftlfloorfrac{m}{2} ight floor) 个数,你需要计算出选择哪些数使其sum和是(mathit k) 的倍数,且sum和最大。
思路:
考虑动态规划来解决:
对于每一行单独处理:
设(dp[i][j]) 代表当前行中选择了(mathit i)个数,sum和对(mathit k)取模后的结果为(mathit j)的最大求和值。
为了让同一个数不被多次选择,我们需要用一个相同的额外数组来滚动解决。
初始时(dp[0][0]=0),
从第(mathit i) 行转移到(i+1)行时 枚举(win[1,m/2],zin[0,k-1]),:(dp1[0][z] = max(dp1[0][z], dp2[w][z]);)
最后(dp1[0][0])就是答案。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m, k;
int a[72][72];
int dp1[72][72];
int dp2[72][72];
int main()
{
#if DEBUG_Switch
freopen("D:\code\input.txt", "r", stdin);
#endif
//freopen("D:\code\output.txt","w",stdout);
n = readint(); m = readint(); k = readint();
repd(i, 1, n) {
repd(j, 1, m) {
a[i][j] = readint();
}
}
repd(i, 0, m) {
repd(j, 0, k) {
dp1[i][j] = -inf;
}
}
dp1[0][0] = 0;
repd(i, 1, n) {
repd(j, 1, m) {
repd(w, 0, m / 2) {
repd(z, 0, k - 1) {
dp2[w][z] = dp1[w][z];
}
}
repd(w, 1, m / 2) {
repd(z, 0, k - 1) {
dp1[w][z] = max(dp1[w][z], dp2[w - 1][(z + a[i][j]) % k] + a[i][j]);
}
}
}
repd(w, 0, m / 2) {
repd(z, 0, k - 1) {
dp2[w][z] = dp1[w][z];
}
}
repd(z, 0, k - 1) {
repd(w, 0, m / 2) {
dp1[0][z] = max(dp1[0][z], dp2[w][z]);
}
}
}
printf("%d
", dp1[0][0] );
return 0;
}