結果

問題 No.2810 Have Another Go (Hard)
ユーザー highlighterhighlighter
提出日時 2024-06-18 22:20:08
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,112 bytes
コンパイル時間 6,641 ms
コンパイル使用メモリ 330,220 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-24 13:32:30
合計ジャッジ時間 7,365 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using mint=modint998244353;

template<typename T>
struct matrix{
	int H,W;
	vector<vector<T>> table;
	matrix(int h,int w) : H(h),W(w){
		table.resize(h,vector<T>(w));
	}
	int size(){
		return H;
	}
	matrix pow(long long N){
		assert(H==W);
		assert(0<=N);
		matrix x=*this;
		matrix r(H,H);
		for(int i=0;i<H;i++){
			r[i][i]=1;
		}
		while(N){
			if(N&1)r*=x;
			x*=x;
			N>>=1;
		}
		return r;
	}
	vector<T>& operator[](int index){
		assert(index<H);
		return table[index];
	}
	matrix& operator+=(const matrix &other){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if((int)(other.table.size())<=i){
					continue;
				}
				if((int)(other.table[i].size())<=j){
					continue;
				}
				(*this)[i][j]+=other.table[i][j];
			}
		}
		return *this;
	}
	matrix operator*=(const matrix &other){
		int h=other.H;
		int w=other.W;
		assert(h==W);
		//結果はH*w行列になる
		matrix result(H,w);
		for(int i=0;i<H;i++){
			for(int k=0;k<W;k++){
				for(int j=0;j<w;j++){
					result.table[i][j]+=table[i][k]*other.table[k][j];
				}
			}
		}
		*this=result;
		return *this;
	}
};

int main(){
	int N,k;
	long long M;
	scanf("%d%lld%d",&N,&M,&k);
	//1,1,2,4,8,16で始まり,前6項を足す線型漸化式に使うvectorを用意しておく
	vector<mint> A(300000);
	A[0]=1;
	A[1]=1;
	A[2]=2;
	A[3]=4;
	A[4]=8;
	A[5]=16;
	for(int i=6;i<300000;i++){
		A[i]=A[i-1]+A[i-2]+A[i-3]+A[i-4]+A[i-5]+A[i-6];
	}
	for(;k--;){
		int C;
		scanf("%d",&C);
		vector<vector<mint>> P(6,vector<mint>(6));
		//P[i][j]:X=iからスタートして,X=N+j(ピッタリ)で終わるサイコロの振り方であって,常にX \not \equiv C (mod N)である場合
		for(int i=0;i<6;i++){
			for(int j=0;j<6;j++){
				if(i==C){
					P[i][j]=0;
					continue;
				}
				if(j==C){
					P[i][j]=0;
					continue;
				}
				if(i<C && j<C){
					P[i][j]=A[C-i]*A[N+j-C];
					P[i][j]=A[N+j-i]-P[i][j];
					continue;
				}
				if(C<j && C<i){
					P[i][j]=A[N+C-i]*A[j-C];
					P[i][j]=A[N+j-i]-P[i][j];
					continue;
				}
				if(i<C && C<j){
					mint res1=A[C-i];
					mint res2=A[N];
					mint res3=A[j-C];
					mint res4=A[N+C-i];
					mint res5=A[N+j-C];
					mint res6=A[N+j-i];
					P[i][j]=res6-res1*res5-res4*res3+res1*res2*res3;
					continue;
				}
				P[i][j]=A[N+j-i];
			}
		}
		matrix<mint> Q(6,6);
		for(int i=0;i<6;i++){
			for(int j=0;j<6;j++){
				Q[i][j]=P[j][i];
			}
		}
		Q=Q.pow(M-2);//to do:M=1の場合に場合分けして書く
		matrix<mint> R(6,1);
		for(int i=0;i<6;i++){
			R[i][0]=P[0][i];
		}
		Q*=R;
		vector<mint> S(6);
		for(int i=0;i<6;i++){
			if(Q[i][0]==0){
				continue;
			}
			for(int j=0;j<6;j++){
				//iからN+jに動く
				//N~N+jの間のCについては無視
				if(i<C){
					mint res=A[C-i]*A[N+j-C];
					res=A[N+j-i]-res;
					S[j]+=res*Q[i][0];
					continue;
				}
				assert(i!=C);
				mint res=A[N+j-i];
				S[j]+=res*Q[i][0];
			}
		}
		mint ans=0;
		for(int i=5;i>=0;i--){
			for(int j=i-1;j>=0;j--){
				S[i]-=S[j];
			}
			ans+=S[i];
		}
		printf("%d\n",ans.val());
	}
}
0