結果
問題 | No.1239 Multiplication -2 |
ユーザー |
![]() |
提出日時 | 2020-09-25 23:23:46 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,669 bytes |
コンパイル時間 | 2,402 ms |
コンパイル使用メモリ | 199,076 KB |
最終ジャッジ日時 | 2025-01-14 21:39:12 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 WA * 17 |
ソースコード
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; #define rep2(i,m,n) for (int i = (m); i < (n); ++i) #define rep(i,n) rep2(i,0,n) typedef long long int ll; typedef long double ld; typedef pair<int, int> P; template<typename T> struct V : vector<T> { using vector<T>::vector; }; V() -> V<int>; V(size_t) -> V<int>; template<typename T> V(size_t, T) -> V<T>; template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); } template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int INF = 1<<30; const ll LINF = 1LL<<61; const ll MOD = 998244353; struct mint { ll x; mint(ll x = 0) : x(x % MOD) {} mint& operator+=(const mint rh) { if((x += rh.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint rh) { if((x += MOD-rh.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint rh) { (x *= rh.x) %= MOD; return *this; } mint operator+(const mint rh) const { return mint(*this) += rh; } mint operator-(const mint rh) const { return mint(*this) -= rh; } mint operator*(const mint rh) const { return mint(*this) *= rh; } mint pow(ll k) const { if(!k) return 1; mint a = x; mint res = 1; while(k) { if(k & 1) res *= a; k >>= 1; a *= a; } return res; } mint inv() const { return pow(MOD-2); } mint& operator/=(const mint rh) { return *this *= rh.inv(); } mint operator/(const mint rh) { return mint(*this) /= rh; } }; long long mpow(long long x, int k, long long MOD = MOD) { long long res = 1; while(k) { if(k & 1) res = (res * x) % MOD; k >>= 1; x = (x * x) % MOD; } return res; } long long modinv(long long a, long long m = MOD) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main() { int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; vector<mint> dp0(6), dp1(6); int ofs = 2; dp1[a[0] + ofs] = 1; for(int i=1; i<n; i++) { vector<mint> dp02(6), dp12(6); for(int j=-2; j<=3; j++) { //区切らない int nj = j * a[i]; if(abs(nj) <= 2) { dp02[nj+ofs] += dp0[j+ofs]; dp12[nj+ofs] += dp1[j+ofs]; } else { dp02[5] += dp0[j+ofs]; dp12[5] += dp1[j+ofs]; } //区切る nj = a[i]; if(j == -2) { dp02[nj+ofs] += dp0[j+ofs] + dp1[j+ofs]; } else { dp02[nj+ofs] += dp0[j+ofs]; } dp12[nj+ofs] += dp1[j+ofs]; } swap(dp0, dp02); swap(dp1, dp12); } mint q = mpow(2, n-1); q = modinv(q.x); mint ans = dp1[0]; rep(i,5) ans += dp0[i]; ans *= q; cout << ans.x << endl; return 0; }