#include <algorithm> #include <cstdio> #include <cstdint> #include <limits> #include <list> #define BEAT "%llu" using beat = unsigned long long; using word = unsigned short; #define HUGE "%lld" using huge = long long; /* typedef beat (* next_t)(beat); void print_binary(beat n) { beat mask = 0x8000000000000000llu; while (mask>1 && !(n & mask)) { mask >>= 1; } while (mask>0) { putchar((n & mask) ? '1' : '0'); mask >>= 1; } } void test(next_t fun) { for (beat n = 0; n<=100; ++n) { beat m = fun(n); printf(HUGE ": ", n); print_binary(n); printf(" -> "); print_binary(m); printf("(" HUGE ")\n", m); } } */ class BitCounter { int buffer[65535]; int count(word n) { word mask = 1; int ones = 0; while (mask && mask<=n) { if (n & mask) { ++ones; } mask <<= 1; } return ones; } public: BitCounter(void) { buffer[0] = 0; for (word w = 1; w; ++w) { buffer[w] = count(w); } } int bits(beat n) const { return buffer[static_cast<word>(n)]+buffer[static_cast<word>(n >> 16)]+buffer[static_cast<word>(n >> 32)] +buffer[static_cast<word>(n >> 48)]; } }; // return smallest number > n with more ones than n in binary // (it will always exist) beat next1(beat n) { beat mask = 1; while (n & mask) { mask <<= 1; } return n | mask; } // return smallest number > n with less ones than n in binary // or 0 if no such number exists beat next0(beat n) { beat mask = 1; int ones = 0; while (mask<=n) { if (n & mask) { n ^= mask; ++ones; } else if (ones>=2) { break; } mask <<= 1; } if (ones>=2) { n |= mask; } else { n = 0; } return n; } beat next(beat n, huge quality) { if (quality>0) { return next1(n); } if (quality<0) { return next0(n); } return 0; } // liczba nut int N; // maksymalna wartość bitu beat M; // jakości kolejnych nut huge a[200]; struct Para { beat m_max; huge total; }; BitCounter bc; void computeForFirstNote(huge quality, std::list <Para>& results) { beat m = 0; results.push_back(Para{m, 0}); while (m = next(m, quality), m!=0 && m<=M) { results.push_back(Para{m, quality*bc.bits(m)}); } } void computeForNextNote(huge quality, std::list <Para>& results, const std::list <Para>& prev_results) { huge best_result = std::numeric_limits<huge>::min(); for (auto it = prev_results.begin(); it!=prev_results.end(); ++it) { auto it_next = std::next(it); beat max = (it_next==prev_results.end()) ? M : it_next->m_max; beat m = it->m_max+1; while (m!=0 && m<=max) { huge result = it->total+quality*bc.bits(m); if (result>best_result) { results.push_back(Para{m, result}); best_result = result; } m = next(m, quality); } } } int main() { if (scanf("%d" BEAT, &N, &M)!=2) return 1; for (int i = 0; i<N; ++i) { if (scanf(HUGE, &a[i])!=1) return 1; } std::list <Para> results, prev_results; computeForFirstNote(a[0], results); for (int i = 1; i<N; ++i) { std::swap(results, prev_results); computeForNextNote(a[i], results, prev_results); prev_results.clear(); } printf(HUGE "\n", results.back().total); }
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 178 179 180 181 182 183 | #include <algorithm> #include <cstdio> #include <cstdint> #include <limits> #include <list> #define BEAT "%llu" using beat = unsigned long long; using word = unsigned short; #define HUGE "%lld" using huge = long long; /* typedef beat (* next_t)(beat); void print_binary(beat n) { beat mask = 0x8000000000000000llu; while (mask>1 && !(n & mask)) { mask >>= 1; } while (mask>0) { putchar((n & mask) ? '1' : '0'); mask >>= 1; } } void test(next_t fun) { for (beat n = 0; n<=100; ++n) { beat m = fun(n); printf(HUGE ": ", n); print_binary(n); printf(" -> "); print_binary(m); printf("(" HUGE ")\n", m); } } */ class BitCounter { int buffer[65535]; int count(word n) { word mask = 1; int ones = 0; while (mask && mask<=n) { if (n & mask) { ++ones; } mask <<= 1; } return ones; } public: BitCounter(void) { buffer[0] = 0; for (word w = 1; w; ++w) { buffer[w] = count(w); } } int bits(beat n) const { return buffer[static_cast<word>(n)]+buffer[static_cast<word>(n >> 16)]+buffer[static_cast<word>(n >> 32)] +buffer[static_cast<word>(n >> 48)]; } }; // return smallest number > n with more ones than n in binary // (it will always exist) beat next1(beat n) { beat mask = 1; while (n & mask) { mask <<= 1; } return n | mask; } // return smallest number > n with less ones than n in binary // or 0 if no such number exists beat next0(beat n) { beat mask = 1; int ones = 0; while (mask<=n) { if (n & mask) { n ^= mask; ++ones; } else if (ones>=2) { break; } mask <<= 1; } if (ones>=2) { n |= mask; } else { n = 0; } return n; } beat next(beat n, huge quality) { if (quality>0) { return next1(n); } if (quality<0) { return next0(n); } return 0; } // liczba nut int N; // maksymalna wartość bitu beat M; // jakości kolejnych nut huge a[200]; struct Para { beat m_max; huge total; }; BitCounter bc; void computeForFirstNote(huge quality, std::list <Para>& results) { beat m = 0; results.push_back(Para{m, 0}); while (m = next(m, quality), m!=0 && m<=M) { results.push_back(Para{m, quality*bc.bits(m)}); } } void computeForNextNote(huge quality, std::list <Para>& results, const std::list <Para>& prev_results) { huge best_result = std::numeric_limits<huge>::min(); for (auto it = prev_results.begin(); it!=prev_results.end(); ++it) { auto it_next = std::next(it); beat max = (it_next==prev_results.end()) ? M : it_next->m_max; beat m = it->m_max+1; while (m!=0 && m<=max) { huge result = it->total+quality*bc.bits(m); if (result>best_result) { results.push_back(Para{m, result}); best_result = result; } m = next(m, quality); } } } int main() { if (scanf("%d" BEAT, &N, &M)!=2) return 1; for (int i = 0; i<N; ++i) { if (scanf(HUGE, &a[i])!=1) return 1; } std::list <Para> results, prev_results; computeForFirstNote(a[0], results); for (int i = 1; i<N; ++i) { std::swap(results, prev_results); computeForNextNote(a[i], results, prev_results); prev_results.clear(); } printf(HUGE "\n", results.back().total); } |