mr_dot_convict

888E-Maximum-Sequence-codeforces-mr.convict

May 5th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.17 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define X         first
  24. #define Y         second
  25. #define sz(x)     (int)x.size()
  26. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  27. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  28. #define bcnt(x)   __builtin_popcount(x)
  29. #define bcntll(x) __builtin_popcountll(x)
  30. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  31. #define un(x)     sort(x.begin(), x.end()), \
  32.   x.erase(unique(x.begin(), x.end()), x.end())
  33. using   ll  =     long long;
  34. using   ull =     unsigned long long;
  35. using   ff  =     long double;
  36. using   pii =     pair<int,int>;
  37. using   pil =     pair<int,ll>;
  38. using   pll =     pair<ll,ll>;
  39. using   vi  =     vector <int>;
  40. using   vl  =     vector<ll>;
  41. using   vvi =     vector <vi>;
  42. using   vvl =     vector <vl>;
  43. using   vp  =     vector <pii>;
  44. using   vvp =     vector <vp>;
  45. typedef tree
  46. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  47. ordered_set;
  48.  
  49. struct chash {
  50.   int operator () (pii x) const { return x.X*31 + x.Y; }
  51. };
  52. gp_hash_table <pii, int, chash> gmp;
  53.  
  54. #if __cplusplus > 201103L
  55. seed_seq seq{
  56.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  57.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  58.     (uint64_t) __builtin_ia32_rdtsc(),
  59.     (uint64_t) (uintptr_t) make_unique<char>().get()
  60. };
  61. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  62. #else
  63. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  64. mt19937 rng(seed);
  65. #endif
  66.  
  67. #ifdef CONVICTION
  68. void __print(int x) {cerr << x;}
  69. void __print(long x) {cerr << x;}
  70. void __print(long long x) {cerr << x;}
  71. void __print(unsigned x) {cerr << x;}
  72. void __print(unsigned long x) {cerr << x;}
  73. void __print(unsigned long long x) {cerr << x;}
  74. void __print(float x) {cerr << x;}
  75. void __print(double x) {cerr << x;}
  76. void __print(long double x) {cerr << x;}
  77. void __print(char x) {cerr << '\'' << x << '\'';}
  78. void __print(const char *x) {cerr << '\"' << x << '\"';}
  79. void __print(const string &x) {cerr << '\"' << x << '\"';}
  80. void __print(bool x) {cerr << (x ? "true" : "false");}
  81.  
  82. template<typename T, typename V>
  83. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  84. template<typename T>
  85. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  86. void _print() {cerr << "]" << endl;}
  87. template <typename T, typename... V>
  88. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  89. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  90. #else
  91. #define debug(x...)
  92. #endif
  93. //         __                                           __
  94. //        (**)                                         (**)
  95. //        IIII                                         IIII
  96. //        ####                                         ####
  97. //        HHHH     Madness comes, and madness goes     HHHH
  98. //        HHHH    An insane place, with insane moves   HHHH
  99. //        ####   Battles without, for battles within   ####
  100. //     ___IIII___        Where evil lives,          ___IIII___
  101. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  102. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  103. // `     }    {     '  just breaking them in    `     }    {     '
  104. //       ) () (  Quickest way out, quickest way wins  ) () (
  105. //       ( :: )      Never disclose, never betray     ( :: )
  106. //       | :: |   Cease to speak or cease to breath   | :: |
  107. //       | )( |        And when you kill a man,       | )( |
  108. //       | || |          you're a murderer            | || |
  109. //       | || |             Kill many                 | || |
  110. //       | || |        and you're a conqueror         | || |
  111. //       | || |        Kill them all ... Ooh..        | || |
  112. //       | || |           Oh you're a God!            | || |
  113. //       ( () )                       -Megadeth       ( () )
  114. //        \  /                                         \  /
  115. //         \/                                           \/
  116. /*}}}*/
  117. /*****************************************************************************/
  118. //Don’t practice until you get it right. Practice until you can’t get it wrong
  119.  
  120. void solve() {
  121.   int n, m;
  122.   cin >> n >> m;
  123.   vector <int> ar(n);
  124.   for (int i = 0; i < n; ++i)
  125.     cin >> ar[i];
  126.  
  127.   vector <int> F, S;
  128.   int __n = n/2;
  129.   for (int mask = 0; mask < (1 << __n); ++mask) {
  130.     int res = 0;
  131.     for (int i = 0; i < __n; ++i)
  132.       if (mask & (1 << i)) res += ar[i], res %= m;
  133.     F.push_back(res);
  134.   }
  135.  
  136.   for (int mask = 0; mask < (1 << (n - __n)); ++mask) {
  137.     int res = 0;
  138.     for (int i = __n; i < n; ++i)
  139.       if (mask & (1 << (i - __n))) res += ar[i], res %= m;
  140.     S.push_back(res);
  141.   }
  142.  
  143.   un(F), un(S);
  144.  
  145.   int ssz = (int)S.size();
  146.   int mx = 0;
  147.   for (int f : F) {
  148.     int rem = m - f - 1;
  149.     int idx = int(lower_bound(S.begin(), S.end(), rem) - S.begin());
  150.     if (idx < ssz) {
  151.       if (S[idx] > rem) --idx;
  152.       if (idx < ssz && idx >= 0) mx = max(mx, (f + S[idx]) % m);
  153.     }
  154.     else
  155.       mx = max(mx, (f + S.back()) % m);
  156.   }
  157.   cout << mx << '\n';
  158. }
  159.  
  160. signed main() {
  161.   IOS; PREC;
  162.   int tc = 1;
  163.   // cin >> tc;
  164.   for (int Tt = 1; Tt <= tc; ++Tt) {
  165.     // cout << "Case #" << Tt << ": ";
  166.     solve();
  167.   }
  168.   return EXIT_SUCCESS;
  169. }
Add Comment
Please, Sign In to add comment