Problem p02662 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \
  int i;                                                                       \
  cin >> i
#define readll(i)                                                              \
  ll i;                                                                        \
  cin >> i
#define readdouble(i)                                                          \
  double i;                                                                    \
  cin >> i
#define readstring(s)                                                          \
  string s;                                                                    \
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 998244353;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return (halfpower * halfpower % modd);
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return ((halfpower * halfpower % modd) * a) % modd;
    }
  }
};

int main() {
  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    readint(s);
    vector<ll> d;
    forr(i, 0, n) {
      readint(kk);
      d.push_back(kk);
    }

    vector<pair<ll, ll>> poss(s + 1, make_pair(0, 0));
    poss[0] = make_pair(1, 0);
    forr(i, 0, n) {
      for (int j = poss.size() - d[i] - 1; j >= 0; --j) {
        auto temp1 = poss[j + d[i]];
        auto temp2 = poss[j];
        temp2.second++;
        int new_d = max(temp1.second, temp2.second);
        if (temp1.second < new_d) {
          temp1.first *= fastpower<ll>(2, new_d - temp1.second);
          temp1.first = temp1.first % modd;
          temp1.second = new_d;
        }
        if (temp2.second < new_d) {
          temp2.first *= fastpower<ll>(2, new_d - temp2.second);
          temp2.first = temp2.first % modd;
          temp2.second = new_d;
        }
        poss[j + d[i]] =
            make_pair((temp1.first + temp2.first) % modd, temp1.second);
      }
    }
    ll ret = (fastpower<ll>(2, n - poss[s].second) * poss[s].first) % modd;

    cout << ret << endl;
  }
  return 0;
}


Fixed submission:


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \
  int i;                                                                       \
  cin >> i
#define readll(i)                                                              \
  ll i;                                                                        \
  cin >> i
#define readdouble(i)                                                          \
  double i;                                                                    \
  cin >> i
#define readstring(s)                                                          \
  string s;                                                                    \
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 998244353;

template <class T> T fastpower(T a, long long n) { // n must be >= 1
  if (n % 2 == 0) {
    T halfpower = fastpower<T>(a, n / 2);
    return (halfpower * halfpower % modd);
  } else {
    if (n == 1) {
      return a;
    } else {
      T halfpower = fastpower<T>(a, n / 2);
      return ((halfpower * halfpower % modd) * a) % modd;
    }
  }
};

int main() {
  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(n);
    readint(s);
    vector<ll> d;
    forr(i, 0, n) {
      readint(kk);
      d.push_back(kk);
    }

    vector<pair<ll, ll>> poss(s + 1, make_pair(0, 0));
    poss[0] = make_pair(1, 0);
    forr(i, 0, n) {
      for (int j = poss.size() - d[i] - 1; j >= 0; --j) {
        auto temp1 = poss[j + d[i]];
        auto temp2 = poss[j];
        temp2.second++;
        int new_d = max(temp1.second, temp2.second);
        if (temp1.second < new_d) {
          temp1.first *= fastpower<ll>(2, new_d - temp1.second);
          temp1.first = temp1.first % modd;
          temp1.second = new_d;
        }
        if (temp2.second < new_d) {
          temp2.first *= fastpower<ll>(2, new_d - temp2.second);
          temp2.first = temp2.first % modd;
          temp2.second = new_d;
        }
        poss[j + d[i]] =
            make_pair((temp1.first + temp2.first) % modd, temp1.second);
      }
    }
    if (n > poss[s].second) {
      ll ret = (fastpower<ll>(2, n - poss[s].second) * poss[s].first) % modd;
      cout << ret << endl;
    } else {
      cout << poss[s].first << endl;
    }
  }
  return 0;
}