// n choose r combination #include #include #include #include #include using namespace std; vector tempData, combLines; int tempSum, maxSum = 0; int N, M, B; // to safe the values from 1st line struct c_unique { int current; c_unique() { current = 0; } int operator()() { return ++current; } } UniqueNumber; void myfunction(int i) { std::cout << i << ' '; } int main() { std::clock_t tStart = std::clock(); ifstream file; string filename; vector > vec; // to keep all the data from file vector tempVec; // to keep data from line filename = "subsetselection.in"; // filename of the file file.open(filename.c_str()); int a, b, c; // Extract data from the file file >> N >> M >> B; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { file >> a;// >> b >> c; tempVec.push_back(a); } //tempVec.push_back(b); //tempVec.push_back(c); vec.push_back(tempVec); tempVec.clear(); } file.close(); //int n = 5; int r = 2; std::vector myints(r); while (r <= N) { std::vector::iterator first = myints.begin(), last = myints.end(); //std::for_each(first, last, myfunction); std::generate(first, last, UniqueNumber); while ((*first) != N - r + 1) { std::vector::iterator mt = last; while (*(--mt) == N - (last - mt) + 1); (*mt)++; while (++mt != last) *mt = *(mt - 1) + 1; //for (int i = 0; i < myints.size(); i++) for(vector::iterator it = myints.begin(); it != myints.end(); it++) { for (int j = 0; j < vec[(*it) - 1].size(); j++) { if (tempData.size() < vec[(*it) - 1].size()) //mySet[data[j]].size()) { tempData.push_back(vec[(*it) - 1][j]); //continue; } else tempData[j] += vec[(*it) - 1][j]; } } // here I need to calculate the sum for the current combination for (vector::iterator calculateCombination = tempData.begin(); calculateCombination != tempData.end(); calculateCombination++) { //cout << *it << " "; int temp = ((*calculateCombination) % (B)); tempSum += (temp * temp); } // to check if the current sum is bigger than the previous one if (maxSum < tempSum) { //cout << "maxSum = " << maxSum << " < tempSum = " << tempSum << endl; maxSum = tempSum; tempSum = 0; combLines.clear(); combLines = myints; } } r++; myints.push_back(0); } ofstream outFile; outFile.open("subsetselection.out"); outFile << combLines.size() << endl; for (vector::iterator it = combLines.begin(); it != combLines.end(); it++) { outFile << (*it) << endl; } outFile.close(); std::printf("Time taken: %.2f us\n", (std::clock() - tStart) / 1.0 / CLOCKS_PER_SEC); return 0; }