Advertisement
Ginger_samurai

Untitled

Sep 13th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bitset>
  4.  
  5. using namespace std;
  6.  
  7. vector<long long> elem;
  8. int n, k;
  9.  
  10. bool check_fits(bitset<57> val, int pref, long long check) {
  11.     bitset<57> checkset(check);
  12.  
  13.     for (int i = 1; i <= pref; i++) {
  14.         if (val[57 - i] && !checkset[57 - i]) {
  15.             return false;
  16.         }
  17.     }
  18.     //cout << "Checfit for " << check << endl;
  19.     return true;
  20. }
  21.  
  22. bool check_for_pref(bitset<57> val, int pref, bool log = false) {
  23.     //if (log)
  24.     //    cout << "Checkprefing with " << val.to_string() << " " << pref << endl;
  25.  
  26.     vector<vector<bool>> dp(n + 1, vector<bool>(k + 1));
  27.     dp[0][0] = true;
  28.     for (int i = 1; i <= n; i++) {
  29.         for (int j = 1; j <= k; j++) {
  30.             long long sum = 0;
  31.             for (int seg = 1; seg <= i && !dp[i][j]; seg++) {
  32.                 sum += elem[i - seg];
  33.                 dp[i][j] = dp[i][j] | (dp[i - seg][j - 1] && check_fits(val, pref, sum));
  34.             }
  35.         }
  36.     }
  37.  
  38.     /*if (log) {
  39.         for (int i = 0; i <= n; i++) {
  40.             for (int j = 0; j <= k; j++) {
  41.                 cout << dp[i][j] << " ";
  42.             }
  43.             cout << endl;
  44.         }
  45.     }*/
  46.  
  47.     return dp[n][k];
  48. }
  49.  
  50. bool check(long long checkval) {
  51.     //cout << "Checking " << checkval << endl;
  52.     bitset<57> val(checkval);
  53.     //cout << val.to_string() << endl;
  54.     for (int i = 1; i <= 57; i++) {
  55.         if (val[57 - i] == 0) {
  56.             bitset<57> newval = val;
  57.             newval[57 - i] = true;
  58.             if (check_for_pref(newval, i)) {
  59.                 //check_for_pref(newval, i, true);
  60.                 return true;
  61.             }
  62.         }
  63.     }
  64.     return false;
  65. }
  66.  
  67.  
  68. int main() {
  69.     cin >> n >> k;
  70.     elem.resize(n);
  71.     for (int i = 0; i < n; i++) {
  72.         cin >> elem[i];
  73.     }
  74.  
  75.     long long l = -1, r = ((unsigned long long)1 << (unsigned long long)56);
  76.     while (r > l + 1) {
  77.         long long m = (l + r) / 2;
  78.         if (check(m)) {
  79.             l = m;
  80.         } else {
  81.             r = m;
  82.         }
  83.     }
  84.     cout << r << endl;
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement