結果

問題 No.1751 Fortune Nim
ユーザー 沙耶花沙耶花
提出日時 2021-11-19 21:49:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 2,203 bytes
コンパイル時間 4,647 ms
コンパイル使用メモリ 272,308 KB
実行使用メモリ 4,760 KB
最終ジャッジ日時 2023-08-30 08:06:07
合計ジャッジ時間 8,094 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 56 ms
4,380 KB
testcase_09 AC 53 ms
4,376 KB
testcase_10 AC 45 ms
4,376 KB
testcase_11 AC 67 ms
4,520 KB
testcase_12 AC 41 ms
4,376 KB
testcase_13 AC 68 ms
4,428 KB
testcase_14 AC 51 ms
4,428 KB
testcase_15 AC 52 ms
4,376 KB
testcase_16 AC 61 ms
4,380 KB
testcase_17 AC 51 ms
4,380 KB
testcase_18 AC 69 ms
4,448 KB
testcase_19 AC 47 ms
4,376 KB
testcase_20 AC 74 ms
4,704 KB
testcase_21 AC 57 ms
4,376 KB
testcase_22 AC 75 ms
4,604 KB
testcase_23 AC 65 ms
4,500 KB
testcase_24 AC 78 ms
4,760 KB
testcase_25 AC 78 ms
4,692 KB
testcase_26 AC 78 ms
4,760 KB
testcase_27 AC 79 ms
4,728 KB
testcase_28 AC 78 ms
4,752 KB
testcase_29 AC 77 ms
4,696 KB
testcase_30 AC 77 ms
4,688 KB
testcase_31 AC 77 ms
4,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

template <class S,
          S (*op0)(S, S),
          S (*e0)(),
		  S (*op1)(S, S),
          S (*e1)()
          >
struct matrix{
	vector<vector<S>> v;
	int _h,_w;
	
	matrix(vector<vector<S>> X){
		v = X;
		_h = X.size();
		_w = 0;
		if(X.size()>0)_w = X[0].size();
	}
	
	matrix(int h,int w){
		v.resize(h,vector<S>(w,e0()));
		_h = h;
		_w = w;
	}
	
	void add_element(int from,int to,S x){
		v[to][from] = op0(v[to][from],x);
	}
	
	matrix e(){
		assert(_h==_w);
		matrix<S,op0,e0,op1,e1> ret(_h,_w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<_w;j++){
				if(i==j)ret.v[i][j] = e1();
				else ret.v[i][j] = e0();
			}
		}
		return ret;
	}
	
	matrix &operator*=(const matrix &another){
		matrix<S,op0,e0,op1,e1> ret(_h,another._w);
		for(int i=0;i<_h;i++){
			for(int j=0;j<another._w;j++){
				ret.v[i][j] = e0();
				for(int k=0;k<_w;k++){
					ret.v[i][j] = op0(ret.v[i][j],op1(v[i][k],another.v[k][j]));
				}
			}
		}

		v = ret.v;
		return (*this);
	}
	
	matrix operator*(const matrix &another)const{
		return (matrix(*this)*=another);
	}
	
	matrix pow(long long cnt){
		matrix<S,op0,e0,op1,e1> ret = e();
		auto temp = *this;
		while(cnt!=0LL){
			if((cnt&1)==1){
				ret *= temp;
			}
			temp *= temp;
			cnt>>=1;
		}
		return ret;
	}
};

mint op0(mint a,mint b){
	return a+b;
}

mint e0(){
	return 0;
}

mint op1(mint a,mint b){
	return a*b;
}

mint e1(){
	return 1;
}

int main(){
	
	int n;
	cin>>n;
	
	vector<long long> a(n);
	long long s=0,sum =0;
	rep(i,n){
		cin>>a[i];
		s ^= a[i];
		sum += a[i];
	}
	
	long long need = 0;
	if(s==0){
		need = sum;
	}
	else{
		need = sum;
		rep(i,n){
			if((a[i]^s)<=a[i]){
				need = min(need,sum - (a[i] - (a[i]^s)) + 1LL);
			}
		}
	}
	
	matrix<mint,op0,e0,op1,e1> M(2,2);
	mint temp = mint(3).inv();
	rep(i,2){
		rep(j,2){
			if(i==j)M.add_element(i,j,temp);
			else M.add_element(i,j,temp*2);
		}
	}
	
	M = M.pow(need);
	
	matrix<mint,op0,e0,op1,e1> MM(2,1);
	MM.v[1][0] = 1;
	
	M *= MM;
	
	cout<<M.v[0][0].val()<<endl;
	
	return 0;
}
0