結果
| 問題 | No.1239 Multiplication -2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-23 13:55:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 31 ms / 2,000 ms |
| コード長 | 3,212 bytes |
| 記録 | |
| コンパイル時間 | 1,674 ms |
| コンパイル使用メモリ | 138,448 KB |
| 最終ジャッジ日時 | 2025-01-15 12:50:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>
using namespace std;
typedef long long ll;
using ull = unsigned long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353;
constexpr int MAX = 1100000;
struct mint {
long long x;
mint(long long x = 0) :x((x% mod + mod) % mod) {}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod - 2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
vector dp(2, vector(2, vector(3, vector<mint>(3))));
vector<mint> tw(n + 1); tw[0] = 1; for (int i = 1; i <= n; i++) tw[i] = tw[i - 1] * 2;
dp[0][0][0][0] = 1;
int cur = 0;
int nxt = 1;
auto init = [&](int idx)->void {
for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) dp[idx][i][j][k] = 0;
};
for (int i = 0; i < n; i++) {
init(nxt);
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
dp[nxt][j][k][0] += dp[cur][j][k][0];
int nj = j ^ (a[i] < 0);
int nk = k; if (abs(a[i]) == 2) nk = min(2, nk + 1); if (a[i] == 0) nk = 2;
if (i)dp[nxt][nj][nk][1] += dp[cur][j][k][0] * tw[i - 1];
else dp[nxt][nj][nk][1] += dp[cur][j][k][0];
dp[nxt][nj][nk][1] += dp[cur][j][k][1];
dp[nxt][j][k][2] += dp[cur][j][k][1] * tw[n - 1 - i];
dp[nxt][j][k][2] += dp[cur][j][k][2];
}
}
swap(cur, nxt);
}
mint res = 0;
res += dp[cur][1][1][1] + dp[cur][1][1][2];
//cout << res.x << "\n";
res *= mint(2).pow(n - 1).inv();
cout << res.x << "\n";
}