結果

問題 No.1239 Multiplication -2
ユーザー 👑 jupirojupiro
提出日時 2020-10-23 13:55:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 3,212 bytes
コンパイル時間 1,658 ms
コンパイル使用メモリ 142,044 KB
実行使用メモリ 5,464 KB
最終ジャッジ日時 2023-09-28 15:22:04
合計ジャッジ時間 4,158 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 17 ms
4,376 KB
testcase_17 AC 26 ms
5,452 KB
testcase_18 AC 26 ms
5,352 KB
testcase_19 AC 27 ms
5,372 KB
testcase_20 AC 27 ms
5,348 KB
testcase_21 AC 26 ms
5,348 KB
testcase_22 AC 26 ms
5,196 KB
testcase_23 AC 20 ms
4,564 KB
testcase_24 AC 21 ms
4,612 KB
testcase_25 AC 16 ms
4,412 KB
testcase_26 AC 18 ms
4,588 KB
testcase_27 AC 9 ms
4,376 KB
testcase_28 AC 25 ms
5,224 KB
testcase_29 AC 27 ms
5,180 KB
testcase_30 AC 13 ms
4,376 KB
testcase_31 AC 18 ms
4,380 KB
testcase_32 AC 29 ms
5,464 KB
testcase_33 AC 21 ms
4,708 KB
testcase_34 AC 20 ms
4,624 KB
testcase_35 AC 10 ms
4,376 KB
testcase_36 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0