zoukankan      html  css  js  c++  java
  • [CF1433F] Zero Remainder Sum

    原题

    You are given a matrix aa of size n×mn×m consisting of integers.

    You can choose no more than (⌊frac{m}{2}⌋) elements in each row. Your task is to choose these elements in such a way that their sum is divisible by (k) and this sum is the maximum.

    In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by (k).

    Note that you can choose zero elements (and the sum of such set is (0)).

    Input

    The first line of the input contains three integers nn, mm and $k (1≤n,m,k≤70) $— the number of rows in the matrix, the number of columns in the matrix and the value of (k). The next nn lines contain mm elements each, where the (j)-th element of the (i)-th row is (a_{i,j }(1≤a_{i,j}≤70)).

    Output

    Print one integer — the maximum sum divisible by (k) you can obtain.

    Examples

    input

    3 4 3
    1 2 3 4
    5 2 2 2
    7 1 1 4
    

    output

    24
    

    input

    5 5 4
    1 2 4 2 1
    3 5 1 2 4
    1 5 7 1 2
    3 8 7 1 2
    8 4 7 1 6
    

    output

    56
    

    Note

    In the first example, the optimal answer is (2) and (4) in the first row, (5) and (2) in the second row and (7) and $4 $ in the third row. The total sum is (2+4+5+2+7+4=24).

    思路

    开四维数组 dp[行][列][当前行选了几个][余数],把每行每个余数的dp最大值用mx[]存下来作为下一行选取0个的dp值,最后输出余数为0的最大值。

    注意不能取的情况continue掉。

    选取的个数从0开始,否则会遗漏掉枚举的这个数作为选取的第一个数的情况,这里卡了好久。

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <iomanip>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <unordered_map>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    #define lowbit(x) (x &(-x))
    #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    
    const int maxn = 77;
    int n, m, k;
    int a[maxn][maxn], mx[maxn], dp[maxn][maxn][maxn][maxn];
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cin >> n >> m >> k;
    	_rep(i, 1, n) {
    		_rep(j, 1, m) cin >> a[i][j];
    	}
    	_rep(i, 1, n) {
    		_rep(j, 0, m) {
    			if (!j) {
    				_rep(r, 0, k - 1) dp[i][j][0][r] = mx[r];
    				continue;
    			}
    			_rep(p, 0, min(j, m / 2)) {
    				_rep(r, 0, k - 1) {
    					dp[i][j][p][(r + a[i][j]) % k] = max(dp[i][j][p][(r + a[i][j]) % k], dp[i][j - 1][p][(r + a[i][j]) % k]);
    					if (!(p == 1 && !r) && !dp[i][j - 1][p - 1][r]) continue;
    					dp[i][j][p][(r + a[i][j]) % k] = max(dp[i][j][p][(r + a[i][j]) % k], dp[i][j - 1][p - 1][r] + a[i][j]);
    					mx[(r + a[i][j]) % k] = max(mx[(r + a[i][j]) % k], dp[i][j][p][(r + a[i][j]) % k]);
    				}
    			}
    		}
    	}
    	cout << mx[0] << endl;
    }
    
  • 相关阅读:
    尽量采用as操作符而不是旧式C风格做强制类型转换
    尽量使用条件属性(Conditional Attribute)而不是#if/#endif预处理
    C#跟踪和调试程序-Debug类使用
    C#使用ConditionalAttribute特性来实现代码调试
    微软认知服务:QnA Maker使用示例
    PHP使用微软认知服务Face API
    微软认知服务识别名人和地标
    基于apache httpclient 调用Face++ API
    认知服务调用如何使用图片的DataURL
    Microsoft Azure Storage Exployer使用指南
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13879752.html
Copyright © 2011-2022 走看看