CSES - Shared codeLink to this code:
https://cses.fi/paste/3ff7373cf56f6af416e923/
/**
Lost Arrow (Aryan V S)
Saturday 2021-01-09
**/
#ifdef LOST_IN_SPACE
# if __cplusplus > 201703LL
# include "lost_pch1.h" // C++20
# elif __cplusplus > 201402LL
# include "lost_pch2.h" // C++17
# else
# include "lost_pch3.h" // C++14
# endif
#else
# include <bits/stdc++.h>
#endif
template <int64_t Modulo>
class modular {
static_assert(Modulo > 0, "Modulo must be positive");
private:
int64_t integer = int64_t();
const int64_t modulo = Modulo;
void normalize () {
if (integer >= modulo or -integer <= modulo) integer %= modulo;
if (integer < 0) integer += modulo;
}
public:
modular (const modular& m) : integer (m()) { }
modular (int64_t integer = { }) : integer (integer) { normalize(); }
modular& operator = (const modular& m)
{ integer = m.integer; return *this; }
int64_t mod () const { return modulo; }
int64_t val () const { return integer; }
int64_t operator () () const { return integer; }
int64_t& operator () () { return integer; }
template <typename T> explicit operator T() const { return static_cast <T> (integer); }
modular& operator += (const modular& other)
{ integer += other(); if (integer >= modulo) integer -= modulo; return *this; }
modular& operator -= (const modular& other)
{ integer -= other(); if (integer < 0) integer += modulo; return *this; }
modular& operator *= (const modular& other)
{ return integer *= other.integer, normalize(), *this; }
modular& operator /= (const modular& other)
{ return *this *= other.extended_euclidean_inverse()(), normalize(), *this; }
modular& operator ++ () { return *this += 1; }
modular& operator -- () { return *this -= 1; }
modular operator ++ (int) { modular result (*this); *this += 1; return result; }
modular operator -- (int) { modular result (*this); *this -= 1; return result; }
modular operator + () const { return *this; }
modular operator - () const { return modular(-integer); }
friend modular operator + (modular self, const modular& other) { return self += other; }
friend modular operator - (modular self, const modular& other) { return self -= other; }
friend modular operator * (modular self, const modular& other) { return self *= other; }
friend modular operator / (modular self, const modular& other) { return self /= other; }
friend bool operator == (const modular& left, const modular& right) { return left() == right(); }
friend bool operator != (const modular& left, const modular& right) { return left() != right(); }
friend bool operator <= (const modular& left, const modular& right) { return left() <= right(); }
friend bool operator >= (const modular& left, const modular& right) { return left() >= right(); }
friend bool operator < (const modular& left, const modular& right) { return left() < right(); }
friend bool operator > (const modular& left, const modular& right) { return left() > right(); }
// Assumes modulo is prime
// Fermat's Little Theorem (https://www.wikiwand.com/en/Fermat%27s_little_theorem)
modular fermat_inverse () const {
modular inverse = *this;
inverse.binary_exponentiate(modulo - 2);
#ifdef LOST_IN_SPACE
if (*this * inverse != 1)
throw std::runtime_error("integer and modulo are not co-prime");
#endif
return inverse;
}
// Assumes modulo is prime
// Euler's Totient Theorem (https://www.wikiwand.com/en/Euler%27s_theorem)
modular euler_inverse () const {
auto m = modulo;
long double totient = modulo;
for (int64_t i = 2; i * i <= m; ++i)
if (m % i == 0) {
while (m % i == 0)
m /= i;
totient *= 1.0L - 1.0L / i;
}
if (m > 1)
totient *= 1.0L - 1.0L / m;
int64_t phi = totient;
modular inverse = *this;
inverse.binary_exponentiate(phi - 1);
#ifdef LOST_IN_SPACE
if (*this * inverse != 1)
throw std::runtime_error("integer and modulo are not co-prime");
#endif
return inverse;
}
// Assumes modulo is co-prime with integer
// Extended Euclidean Algorithm (https://www.wikiwand.com/en/Extended_Euclidean_algorithm)
modular extended_euclidean_inverse () const {
int64_t u = 0, v = 1;
int64_t a = integer, b = modulo;
while (a != 0) {
int64_t t = b / a;
b -= t * a;
u -= t * v;
std::swap(a, b);
std::swap(u, v);
}
#ifdef LOST_IN_SPACE
if (b != 1)
throw std::runtime_error("integer and modulo are not co-prime");
#endif
return {u};
}
// Assumes power is non-negative
modular& binary_exponentiate (int64_t power) {
auto base = *this;
*this = 1;
while (power > 0) {
if (power & 1)
*this *= base;
base *= base;
power >>= 1;
}
return *this;
}
modular& binary_exponentiate (const modular& power) {
return binary_exponentiate(power());
}
std::string to_string () const { return std::to_string(integer); }
friend std::ostream& operator << (std::ostream& stream, const modular& m)
{ return stream << m(); }
friend std::istream& operator >> (std::istream& stream, modular& m)
{ stream >> m(); m.normalize(); return stream; }
};
namespace std {
template <int64_t U, typename V>
inline modular <U> pow (modular <U> m, const V& power)
{ return m.binary_exponentiate(power); }
template <int64_t U>
inline std::string to_string (const modular <U>& m)
{ return m.to_string(); }
}
using mod998244353 = modular <998244353>;
using mod1000000007 = modular <1000000007>;
constexpr bool test_cases = false;
void solve () {
int n, x;
std::cin >> n >> x;
std::vector <int> coins (n);
for (int i = 0; i < n; ++i)
std::cin >> coins [i];
std::vector <mod1000000007> dp (x + 1);
dp [0] = 1;
for (int i = 1; i <= x; ++i)
for (int c : coins)
if (i - c >= 0)
dp [i] += dp [i - c];
std::cout << dp [x] << '\n';
}
int main () {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
std::cerr.precision(10);
std::cout << std::fixed << std::boolalpha;
std::cerr << std::fixed << std::boolalpha;
int32_t cases = 1;
if (test_cases)
std::cin >> cases;
while (cases--)
solve();
return 0;
}
// g++.exe -Wall -Weffc++ -Wextra -pedantic -std=c++20 -g -D_GLIBCXX_DEBUG -DLOST_IN_SPACE -H
// Replace failing with learning
// Replace overthinking with action
// Replace blame with responsibility
// Replace toxic friends with mentors
// Replace complaining with gratitude
// Replace netflix marathons with sleep
// Replace fake influencers with inspiring creators