#include <iostream>
#include <algorithm> // std::fill
#include <vector> // std::vector
#include <iterator>
using namespace std;
// class generator:
struct c_unique {
int current;
c_unique() {current=0;}
int operator()() {return ++current;}
} UniqueNumber;
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if ( !v.empty() ) {
//out << "["
std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
//out << "\b\b]";
}
return out;
}
#include <functional>
#include <numeric>
long long int branchnbound(
const vector<int> &seq,
long long int N
//, std::function<long long int(vector<int>&)> calcfnc
//, std::function<bool(vector<int>&, long long int curval)> cutbranchfnc
) {
int product = std::accumulate(seq.begin(), seq.end(), 1, std::multiplies<int>());
//cout<<seq<<" = "<<product<<endl;
if(product <= N) {
return product;
} else {
long long int acc = 1;
for(auto el : seq) {
vector<int> seq2(seq);
seq2.erase(remove(seq2.begin(), seq2.end(), el), seq2.end());
long long int res = branchnbound(seq2, N);
if(res>N) res = 1;
acc = max(acc, res);
}
return acc;
}
}
int main() {
long long int n, k;
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
vector<int> vec;
while(cin>>n) vec.push_back(n);
// #define UNIQUE
#ifdef UNIQUE // += O(n^2)
std::sort(vec.begin(), vec.end());
auto last = std::unique(vec.begin(), vec.end());
vec.erase(last, vec.end());
#endif
cout<<branchnbound(vec, k)<<endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> #include <algorithm> // std::fill #include <vector> // std::vector #include <iterator> using namespace std; // class generator: struct c_unique { int current; c_unique() {current=0;} int operator()() {return ++current;} } UniqueNumber; template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) { if ( !v.empty() ) { //out << "[" std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, " ")); //out << "\b\b]"; } return out; } #include <functional> #include <numeric> long long int branchnbound( const vector<int> &seq, long long int N //, std::function<long long int(vector<int>&)> calcfnc //, std::function<bool(vector<int>&, long long int curval)> cutbranchfnc ) { int product = std::accumulate(seq.begin(), seq.end(), 1, std::multiplies<int>()); //cout<<seq<<" = "<<product<<endl; if(product <= N) { return product; } else { long long int acc = 1; for(auto el : seq) { vector<int> seq2(seq); seq2.erase(remove(seq2.begin(), seq2.end(), el), seq2.end()); long long int res = branchnbound(seq2, N); if(res>N) res = 1; acc = max(acc, res); } return acc; } } int main() { long long int n, k; std::ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; vector<int> vec; while(cin>>n) vec.push_back(n); // #define UNIQUE #ifdef UNIQUE // += O(n^2) std::sort(vec.begin(), vec.end()); auto last = std::unique(vec.begin(), vec.end()); vec.erase(last, vec.end()); #endif cout<<branchnbound(vec, k)<<endl; return 0; } |
English