Modulus of subsequence sum

Xiaoqiang has an integer array a of length n and a number m. He needs to select a subsequence in the array a so that the sum of this subsequence is the largest after modulo m. The subsequence can be empty. May I ask this result? What is the maximum

/*  
input:
    第一行 两个正整数n, m
	第二行 n个正整数a1,a2,a3,...,an
	1 <= n <= 35, 1 <= m <= 1000000000, 1 <= ai <= 1000000000
	
output:
    输出一行一个整数,代表答案
*/

#include <iostream>
#include <vector>
using namespace std;

void getSubseq(int index, vector<int>& arr, vector<vector<int>>& combineArr, vector<int> arr2) {
    
    
	if (index >= arr.size()) {
    
    
		combineArr.push_back(arr2);
		return;
	}
	getSubseq(index + 1, arr, combineArr, arr2);
	arr2.push_back(arr.at(index));
	getSubseq(index + 1, arr, combineArr, arr2);
}

int getMaxModulo(vector<vector<int>>& combineArr, int m) {
    
    
	int max = 0;
	for (vector<int>& e : combineArr) {
    
    
		if (e.empty()) {
    
    
			continue;
		}
		long long sum = 0;
		for (int i : e) {
    
    
			sum += i;
		}
		max = max >= (sum % m) ? max : (sum % m);
	}
	return max;
}

int main() {
    
    
	int n = 1;
	int m = 1;
	cin >> n >> m;
	vector<int> inputArr;
	for (int i = 0; i < n; i++) {
    
    
		int value = 0;
		cin >> value;
		inputArr.push_back(value);
	}
	vector<vector<int>> combineArr;
	getSubseq(0, inputArr, combineArr, vector<int>());
	cout << getMaxModulo(combineArr, m) << endl;
	system("pause");
	return 0;
}

If there is any infringement, please contact to delete it. If there is an error, please correct me, thank you

Guess you like

Origin blog.csdn.net/xiao_ma_nong_last/article/details/105734193