結果

問題 No.1239 Multiplication -2
ユーザー okok
提出日時 2020-09-25 23:57:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,744 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 83,144 KB
実行使用メモリ 4,820 KB
最終ジャッジ日時 2023-09-10 17:15:05
合計ジャッジ時間 3,033 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 20 ms
4,380 KB
testcase_17 AC 31 ms
4,816 KB
testcase_18 AC 41 ms
4,644 KB
testcase_19 AC 27 ms
4,636 KB
testcase_20 AC 33 ms
4,660 KB
testcase_21 AC 49 ms
4,696 KB
testcase_22 AC 37 ms
4,752 KB
testcase_23 AC 37 ms
4,376 KB
testcase_24 AC 27 ms
4,380 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 32 ms
4,380 KB
testcase_27 AC 15 ms
4,376 KB
testcase_28 AC 46 ms
4,388 KB
testcase_29 AC 51 ms
4,380 KB
testcase_30 AC 19 ms
4,376 KB
testcase_31 AC 28 ms
4,384 KB
testcase_32 AC 47 ms
4,820 KB
testcase_33 AC 32 ms
4,376 KB
testcase_34 AC 30 ms
4,380 KB
testcase_35 AC 16 ms
4,380 KB
testcase_36 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 998244353; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

long long power(long long x, long long n){
	long long ans = 1;
	n--;
	
	for(;n > 0;n >>= 1, x *= x, ans %= MOD, x %= MOD)
		if(n&1)ans*=x;
	return ans % MOD;
}

long long power2(long long x, long long n){
	long long ans = 1;
	
	for(;n > 0;n >>= 1, x *= x, ans %= MOD, x %= MOD)
		if(n&1)ans*=x;
	return ans % MOD;
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
	vector<int> a;
	int ans = 0;
	
	cin>>N;
	
	a.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>a[i];
	}
	
	for(int i = 0; i < N; i++){
		if(abs(a[i]) != 2) continue;
		int con0[2] = {0,power(2,i)};
		int con1[2] = {0,power(2,N-i-1)};		
		
		for(int j = i+1, now = 1; j < N; j++){
			if(abs(a[j]) != 1) break;;
			now *= a[j];
			
			if(now == -1) {
				con1[0] += power(2, N-j-1);
				con1[0] %= MOD;
			} else {
				con1[1] += power(2, N-j-1);
				con1[1] %= MOD;
				
			}
		}
		
		for(int j = i-1, now = 1; j >= 0; j--){
			if(abs(a[j]) != 1) break;
			now *= a[j];
			
			if(now == -1) {
				con0[0] += power(2, j);
				con0[0] %= MOD;
			} else {
				con0[1] += power(2, j);
				con0[1] %= MOD;
			}
		}
		
		if(a[i] == -2) {
			ans += con1[0] * con0[0];
			ans += con1[1] * con0[1];
			ans %= MOD;
		} else if(a[i] == 2) {
			ans += con1[0] * con0[1];
			ans += con1[1] * con0[0];
			ans %= MOD;
		}
	}
	
	ans *= power2(power2(2, N-1), MOD-2);
	ans %= MOD;
	
	cout<<ans<<endl;
	
	return 0;
}
0