結果
問題 | No.1861 Required Number |
ユーザー |
|
提出日時 | 2022-03-04 22:00:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 245 ms / 2,500 ms |
コード長 | 2,443 bytes |
コンパイル時間 | 2,176 ms |
コンパイル使用メモリ | 199,384 KB |
最終ジャッジ日時 | 2025-01-28 05:18:25 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 MLE * 4 |
ソースコード
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define STRINGIFY(n) #n #define TOSTRING(n) STRINGIFY(n) #define PREFIX "#" TOSTRING(__LINE__) "| " #define debug(x) \ { \ std::cout << PREFIX << #x << " = " << x << std::endl; \ } std::ostream& output_indent(std::ostream& os, int ind) { for(int i = 0; i < ind; i++) os << " "; return os; } template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p); template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v); template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) { return (os << "(" << p.first << ", " << p.second << ")"); } template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { os << "["; for(int i = 0;i < v.size();i++) os << v[i] << ", "; return (os << "]"); } template<class T> static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); } template<class... Tail> static inline auto ndvec(size_t&& n, Tail&&... tail) { return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...)); } template<class Cond> struct chain { Cond cond; chain(Cond cond) : cond(cond) {} template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); } int main() { i64 N, K; cin >> N >> K; vector<i64> A(N); rep(i,0,N) cin >> A[i]; const i64 MOD = 998244353; auto dp = ndvec(N + 1, K + 1, 0ll); dp[0][0] = 1; rep(i,0,N) { rep(k,0,K + 1) { (dp[i + 1][k] += dp[i][k]) %= MOD; if(k - A[i] >= 0) { (dp[i + 1][k] += dp[i][k - A[i]]) %= MOD; } } } auto rdp = ndvec(N + 1, K + 1, 0ll); rdp[N][0] = 1; for(int i = N; i --> 0;) { rep(k,0,K + 1) { (rdp[i][k] += rdp[i + 1][k]) %= MOD; if(k - A[i] >= 0) { (rdp[i][k] += rdp[i + 1][k - A[i]]) %= MOD; } } } i64 ans = 0; if(dp[N][K] == 0) { cout << -1 << endl; return 0; } rep(i,0,N) { i64 cnt = 0; rep(k,0,K - A[i] + 1) { (cnt += dp[i][k] * rdp[i + 1][K - k - A[i]]) %= MOD; } if(cnt == dp[N][K]) { ans++; } } cout << ans << endl; }