Advertisement
tien_noob

Bignum - Optimization (LMH)

Jun 21st, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #define taskname "BALANCE"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <array>
  7. #include <iomanip>
  8. using namespace std;
  9. using lli = long long;
  10. using ld = long double;
  11. const int maxN = 100;
  12. const int maxM = maxN * (maxN + 1) / 2;
  13. const lli Radix = 1e18;
  14. const int gd = 18;
  15. const int nd = 3;
  16.  
  17.  
  18. int n, m, k;
  19.  
  20. struct TNumber
  21. {
  22.     array<lli, nd> d; //d[0] + d[1] * Radix + d[2] * Radix ^ 2.
  23.     TNumber& operator = (lli x)
  24.     {
  25.         fill(d.begin(), d.end(), 0);
  26.         d[0] = x;
  27.         return *this;
  28.     }
  29.     TNumber operator + (const TNumber& other) const
  30.     {
  31.         TNumber res;
  32.         lli carry = 0LL;
  33.         for (int i = 0; i < nd; ++i)
  34.         {
  35.             carry += d[i] + other.d[i];
  36.             res.d[i] = carry % Radix;
  37.             carry /= Radix;
  38.         }
  39.         return res;
  40.     }
  41.     void Print() const
  42.     {
  43.         int i = nd - 1;
  44.         while (i > 0 && d[i] == 0) --i;
  45.         cout << d[i];
  46.         for (--i; i >= 0; --i)
  47.             cout << setw(gd) << setfill('0') << d[i];
  48.     }
  49. };
  50.  
  51.  
  52. TNumber f[maxM + 1], g[maxM + 1];
  53.  
  54. void ReadInput()
  55. {
  56.     cin >> n >> m;
  57.     k = n * (n + 1) / 2;
  58. }
  59.  
  60. void Solve()
  61. {
  62.     f[0] = 1;
  63.     for (int y = 1; y <= k; ++y)
  64.         f[y] = 0;
  65.     for (int x = 1; x <= n; ++x)
  66.     {
  67.         for (int y = 0; y <= k; ++y)
  68.         {
  69.             g[y] = f[y] + f[abs(x - y)];
  70.             if (x + y <= k) g[y] = g[y] + f[x + y];
  71.         }
  72.         swap(f, g);
  73.     }
  74.     f[m].Print();
  75. }
  76.  
  77. int main()
  78. {
  79.     ios_base::sync_with_stdio(false);
  80.     cin.tie(nullptr);
  81.     //freopen(taskname".INP", "r", stdin);
  82.     //freopen(taskname".OUT", "w", stdout);
  83.     ReadInput();
  84.     Solve();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement