#include <algorithm> #include <cstdio> #include <vector> // Logging. const bool logging_enabled = false; // Constants. const int MAX_BITS = 61; const long long INF = 1000000000000000001LL; const int MAX_NUMS = 201; // Newton symbol. long long C[MAX_BITS+1][MAX_BITS+1]; // How many numbers <= n with b bits are there? long long CountNumbersWithBits2(long long n, int b) { if (logging_enabled) printf("Count(%lld, %d)\n", n, b); // Corner case. if (n == 0) { if (b == 0) return 1; else return 0; } long long res = 0; auto num_bits = 0; for (int i = 0; i < MAX_BITS; ++i) { if ((1LL << i) & n) ++num_bits; } if (logging_enabled) printf("num_bits(%lld) = %d\n", n, num_bits); int pos = 0; int passed_bits = 0; while (n) { long long current_bit = n & 1; n >>= 1; if (current_bit) { ++passed_bits; if (b - num_bits + passed_bits >= 0) { res += C[pos][b - num_bits + passed_bits]; } } ++pos; } if (num_bits == b) ++res; if (logging_enabled) printf(" = %lld\n", res); return res; } // How many numbers <= n with b bits are there? long long CountNumbersWithBits(long long n, int b) { // Corner case. if (n == 0) { if (b == 0) return 1; else return 0; } std::vector<bool> bits; while (n) { bits.push_back(n & 1); n >>= 1; } long long res = 0; // Starting with the most significant bit. std::reverse(bits.begin(), bits.end()); int passed_bits = 0; for (int i = 0; i < bits.size() && passed_bits <= b; ++i) { if (bits[i]) { // Assume we flipped the bit to 0. res += C[bits.size() - i - 1][b - passed_bits]; ++passed_bits; } } if (passed_bits == b) ++res; return res; } // What is the lowest number higher than n with b bits? long long NextHigherWithBits(long long n, int b) { // printf("NextHigherWithBits(%lld, %d)\n", n, b); long long base = CountNumbersWithBits2(n, b); long long l = n + 1, u = INF, res = -1; while (l <= u) { long long m = (l + u) / 2; if (CountNumbersWithBits2(m, b) > base) { res = m; u = m - 1; } else { l = m + 1; } } return res; } int n; long long m; long long a[MAX_NUMS]; // (value, last multiplier) using Solution = std::pair<long long, long long>; std::vector<Solution> dp[MAX_NUMS][MAX_BITS]; int main() { // Precalculate C(n, k). C[0][0] = 1; for (int i = 1; i <= MAX_BITS; ++i) { for (int j = 0; j <= i; ++j) { C[i][j] = C[i-1][j]; if (j > 0) C[i][j] += C[i-1][j-1]; } } /* long long n; int b; scanf("%lld%d", &n, &b); printf("%lld\n", NextHigherWithBits(n, b)); */ scanf("%d%lld", &n, &m); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); if (logging_enabled) printf("Read input.\n"); // Initialize dp[0]. dp[0][0].push_back({0LL, 0LL}); for (int i = 1; i < MAX_BITS; ++i) { long long current_last = 0; for (int sol_idx = 0; sol_idx < n; ++sol_idx) { current_last = NextHigherWithBits(current_last, i); if (current_last == -1 || current_last > m) break; dp[0][i].push_back({i*a[0], current_last}); if (logging_enabled) printf("Pushing {%lld, %lld} to dp[%d][%d]\n", i*a[0], current_last, 0, i); } } // Calculate dp[1..n-1]. for (int i = 1; i < n; ++i) { for (int b = 0; b < MAX_BITS; ++b) { // Calculate dp[i][b]. for (int c = 0; c < MAX_BITS; ++c) { for (const auto& prev_sol : dp[i-1][c]) { Solution sol; sol.second = NextHigherWithBits(prev_sol.second, b); if (sol.second != -1 && sol.second <= m) { sol.first = prev_sol.first + b * a[i]; dp[i][b].push_back(sol); } } } std::sort(dp[i][b].begin(), dp[i][b].end()); std::reverse(dp[i][b].begin(), dp[i][b].end()); if (dp[i][b].size() > n) { dp[i][b].erase(dp[i][b].begin() + n, dp[i][b].end()); } if (!dp[i][b].empty() && logging_enabled) { if (logging_enabled) printf("Calculated non-empty dp[%d][%d]\n", i, b); for (const auto& sol : dp[i][b]) { if (logging_enabled) printf("{%lld, %lld}\n", sol.first, sol.second); } } } } long long res = -INF; for (int b = 0; b < MAX_BITS; ++b) { for (const auto& sol : dp[n-1][b]) { res = std::max(res, sol.first); } } printf("%lld\n", res); 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <algorithm> #include <cstdio> #include <vector> // Logging. const bool logging_enabled = false; // Constants. const int MAX_BITS = 61; const long long INF = 1000000000000000001LL; const int MAX_NUMS = 201; // Newton symbol. long long C[MAX_BITS+1][MAX_BITS+1]; // How many numbers <= n with b bits are there? long long CountNumbersWithBits2(long long n, int b) { if (logging_enabled) printf("Count(%lld, %d)\n", n, b); // Corner case. if (n == 0) { if (b == 0) return 1; else return 0; } long long res = 0; auto num_bits = 0; for (int i = 0; i < MAX_BITS; ++i) { if ((1LL << i) & n) ++num_bits; } if (logging_enabled) printf("num_bits(%lld) = %d\n", n, num_bits); int pos = 0; int passed_bits = 0; while (n) { long long current_bit = n & 1; n >>= 1; if (current_bit) { ++passed_bits; if (b - num_bits + passed_bits >= 0) { res += C[pos][b - num_bits + passed_bits]; } } ++pos; } if (num_bits == b) ++res; if (logging_enabled) printf(" = %lld\n", res); return res; } // How many numbers <= n with b bits are there? long long CountNumbersWithBits(long long n, int b) { // Corner case. if (n == 0) { if (b == 0) return 1; else return 0; } std::vector<bool> bits; while (n) { bits.push_back(n & 1); n >>= 1; } long long res = 0; // Starting with the most significant bit. std::reverse(bits.begin(), bits.end()); int passed_bits = 0; for (int i = 0; i < bits.size() && passed_bits <= b; ++i) { if (bits[i]) { // Assume we flipped the bit to 0. res += C[bits.size() - i - 1][b - passed_bits]; ++passed_bits; } } if (passed_bits == b) ++res; return res; } // What is the lowest number higher than n with b bits? long long NextHigherWithBits(long long n, int b) { // printf("NextHigherWithBits(%lld, %d)\n", n, b); long long base = CountNumbersWithBits2(n, b); long long l = n + 1, u = INF, res = -1; while (l <= u) { long long m = (l + u) / 2; if (CountNumbersWithBits2(m, b) > base) { res = m; u = m - 1; } else { l = m + 1; } } return res; } int n; long long m; long long a[MAX_NUMS]; // (value, last multiplier) using Solution = std::pair<long long, long long>; std::vector<Solution> dp[MAX_NUMS][MAX_BITS]; int main() { // Precalculate C(n, k). C[0][0] = 1; for (int i = 1; i <= MAX_BITS; ++i) { for (int j = 0; j <= i; ++j) { C[i][j] = C[i-1][j]; if (j > 0) C[i][j] += C[i-1][j-1]; } } /* long long n; int b; scanf("%lld%d", &n, &b); printf("%lld\n", NextHigherWithBits(n, b)); */ scanf("%d%lld", &n, &m); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); if (logging_enabled) printf("Read input.\n"); // Initialize dp[0]. dp[0][0].push_back({0LL, 0LL}); for (int i = 1; i < MAX_BITS; ++i) { long long current_last = 0; for (int sol_idx = 0; sol_idx < n; ++sol_idx) { current_last = NextHigherWithBits(current_last, i); if (current_last == -1 || current_last > m) break; dp[0][i].push_back({i*a[0], current_last}); if (logging_enabled) printf("Pushing {%lld, %lld} to dp[%d][%d]\n", i*a[0], current_last, 0, i); } } // Calculate dp[1..n-1]. for (int i = 1; i < n; ++i) { for (int b = 0; b < MAX_BITS; ++b) { // Calculate dp[i][b]. for (int c = 0; c < MAX_BITS; ++c) { for (const auto& prev_sol : dp[i-1][c]) { Solution sol; sol.second = NextHigherWithBits(prev_sol.second, b); if (sol.second != -1 && sol.second <= m) { sol.first = prev_sol.first + b * a[i]; dp[i][b].push_back(sol); } } } std::sort(dp[i][b].begin(), dp[i][b].end()); std::reverse(dp[i][b].begin(), dp[i][b].end()); if (dp[i][b].size() > n) { dp[i][b].erase(dp[i][b].begin() + n, dp[i][b].end()); } if (!dp[i][b].empty() && logging_enabled) { if (logging_enabled) printf("Calculated non-empty dp[%d][%d]\n", i, b); for (const auto& sol : dp[i][b]) { if (logging_enabled) printf("{%lld, %lld}\n", sol.first, sol.second); } } } } long long res = -INF; for (int b = 0; b < MAX_BITS; ++b) { for (const auto& sol : dp[n-1][b]) { res = std::max(res, sol.first); } } printf("%lld\n", res); return 0; } |