結果

問題 No.2810 Have Another Go (Hard)
ユーザー highlighterhighlighter
提出日時 2024-06-24 15:07:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 810 ms / 3,000 ms
コード長 5,226 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 87,452 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 15:08:22
合計ジャッジ時間 21,405 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
6,812 KB
testcase_01 AC 554 ms
6,940 KB
testcase_02 AC 329 ms
6,940 KB
testcase_03 AC 398 ms
6,940 KB
testcase_04 AC 138 ms
6,940 KB
testcase_05 AC 83 ms
6,940 KB
testcase_06 AC 126 ms
6,944 KB
testcase_07 AC 640 ms
6,940 KB
testcase_08 AC 63 ms
6,940 KB
testcase_09 AC 305 ms
6,944 KB
testcase_10 AC 798 ms
6,940 KB
testcase_11 AC 782 ms
6,940 KB
testcase_12 AC 798 ms
6,944 KB
testcase_13 AC 771 ms
6,940 KB
testcase_14 AC 799 ms
6,944 KB
testcase_15 AC 803 ms
6,940 KB
testcase_16 AC 781 ms
6,944 KB
testcase_17 AC 810 ms
6,940 KB
testcase_18 AC 775 ms
6,940 KB
testcase_19 AC 804 ms
6,944 KB
testcase_20 AC 775 ms
6,940 KB
testcase_21 AC 792 ms
6,940 KB
testcase_22 AC 797 ms
6,944 KB
testcase_23 AC 771 ms
6,944 KB
testcase_24 AC 794 ms
6,940 KB
testcase_25 AC 800 ms
6,940 KB
testcase_26 AC 770 ms
6,940 KB
testcase_27 AC 800 ms
6,940 KB
testcase_28 AC 779 ms
6,940 KB
testcase_29 AC 786 ms
6,944 KB
testcase_30 AC 5 ms
6,944 KB
testcase_31 AC 5 ms
6,944 KB
testcase_32 AC 3 ms
6,940 KB
testcase_33 AC 10 ms
6,944 KB
testcase_34 AC 6 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<cstring>
#include<iterator>
#include<atcoder/modint>
using namespace std;
using namespace atcoder;
using mint=modint998244353;

int mem=0;
mint Q_data[62][7];

mint Bostan_Mori(long long N){
	mint Q[7];
	if(mem>0){
		copy(begin(Q_data[0]),end(Q_data[0]),begin(Q));
	}
	if(mem==0){
		Q[0]=1;
		for(int i=1;i<=6;i++){
			Q[i]=-1;
		}
		copy(begin(Q),end(Q),begin(Q_data[0]));
		mem++;
	}
	mint P[6];
	P[0]=1;
	for(int i=1;i<6;i++){
		P[i]=0;
	}
	int count=0;
	while(N>0){
		count++;
		if(mem<=count){
			mint nQ[7];
			for(int i=0;i<=6;i++){
				nQ[i]=0;
				for(int j=max(0,2*i-6);j<=min(2*i,6);j++){
					if(j%2==0){
						nQ[i]+=Q[j]*Q[2*i-j];
						continue;
					}
					nQ[i]-=Q[j]*Q[2*i-j];
				}
			}
			copy(begin(nQ),end(nQ),begin(Q_data[mem]));
			mem++;
		}
		mint nP[6];
		if(N%2==0){
			for(int i=0;i<12;i+=2){
				nP[i/2]=0;
				for(int j=max(0,i-5);j<=min(6,i);j++){
					if(j%2==0){
						nP[i/2]+=Q[j]*P[i-j];
						continue;
					}
					nP[i/2]-=Q[j]*P[i-j];
				}
			}
			copy(begin(nP),end(nP),begin(P));
		}
		if(N%2==1){
			for(int i=1;i<12;i+=2){
				nP[i/2]=0;
				for(int j=max(0,i-5);j<=min(6,i);j++){
					if(j%2==0){
						nP[i/2]+=Q[j]*P[i-j];
						continue;
					}
					nP[i/2]-=Q[j]*P[i-j];
				}
			}
			copy(begin(nP),end(nP),begin(P));
		}
		copy(begin(Q_data[count]),end(Q_data[count]),begin(Q));
		N/=2;
		continue;
	}
	return P[0];
}

template<typename T>
struct matrix{
	int H,W;
	T table[6][6];
	matrix(int h,int w) : H(h),W(w){
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				table[i][j]=0;
			}
		}
	}
	int size(){
		return H;
	}
	matrix pow(long long N){
		matrix x=*this;
		matrix r(H,H);
		for(int i=0;i<H;i++){
			r.table[i][i]=1;
		}
		while(N){
			if(N&1)r*=x;
			x*=x;
			N/=2;
		}
		return r;
	}
	matrix& operator+=(const matrix &other){
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(other.H<=i){
					continue;
				}
				if(other.W<=j){
					continue;
				}
				(*this).table[i][j]+=other.table[i][j];
			}
		}
		return *this;
	}
	matrix operator*=(const matrix &other){
		int h=other.H;
		int w=other.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 k;
	long long N,M;
	scanf("%lld%lld%d",&N,&M,&k);
	//1,1,2,4,8,16で始まり,前6項を足す線型漸化式に使うvectorを用意しておく
	mint vec[6];
	for(int i=0;i<6;i++){
		vec[i]=Bostan_Mori(N*M+i);
	}
	mint W=0;
	for(int i=5;i>=0;i--){
		for(int j=i-1;j>=0;j--){
			vec[i]-=vec[j];
		}
		W+=vec[i];
	}
	for(;k--;){
		int C;
		scanf("%d",&C);
		matrix<mint> Q(6,6);
		matrix<mint> R(6,1);
		//P[i][j]:X=iからスタートして,X=N+j(ピッタリ)で終わるサイコロの振り方であって,常にX \not \equiv C (mod N)である場合
		for(int j=0;j<6;j++){
			mint P_sub[6];
			memset(P_sub,0,6*sizeof(mint));
			for(int i=5;i>=0;i--){
				if(i==C){
					continue;
				}
				if(j==C){
					continue;
				}
				if(i<C && j<C){
					Q.table[j][i]=Bostan_Mori(C-i);
					Q.table[j][i]*=Bostan_Mori(N+j-C);
					Q.table[j][i]=Bostan_Mori(N+j-i)-Q.table[j][i];
					if(i==0){
						R.table[j][i]=Q.table[j][i];
					}
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=Q.table[j][i];
					}
					Q.table[j][i]-=P_sub[i];
					continue;
				}
				if(C<j && C<i){
					Q.table[j][i]=Bostan_Mori(N+C-i);
					Q.table[j][i]*=Bostan_Mori(j-C);
					Q.table[j][i]=Bostan_Mori(N+j-i)-Q.table[j][i];
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=Q.table[j][i];
					}
					Q.table[j][i]-=P_sub[i];
					continue;
				}
				if(i<C && C<j){
					mint res1=Bostan_Mori(C-i);
					mint res2=Bostan_Mori(N);
					mint res3=Bostan_Mori(j-C);
					mint res4=Bostan_Mori(N+C-i);
					mint res5=Bostan_Mori(N+j-C);
					mint res6=Bostan_Mori(N+j-i);
					Q.table[j][i]=res6-res1*res5-res4*res3+res1*res2*res3;
					if(i==0){
						R.table[j][i]=Q.table[j][i];
					}
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=Q.table[j][i];
					}
					Q.table[j][i]-=P_sub[i];
					continue;
				}
				Q.table[j][i]=Bostan_Mori(N+j-i);
				for(int ii=i-1;ii>=0;ii--){
					P_sub[ii]+=Q.table[j][i];
				}
				Q.table[j][i]-=P_sub[i];
			}
		}
		Q=Q.pow(M-2);
		Q*=R;
		mint S[6];
		for(int j=0;j<6;j++){
			S[j]=0;
			mint S_sub[6];
			memset(S_sub,0,6*sizeof(mint));
			for(int i=5;i>=0;i--){
				if(Q.table[i][0]==0){
					continue;
				}
				//iからN+jに動く
				//N~N+jの間のCについては無視
				//i->i+1->N+jみたいな重複に注意
				//S[j]+=Q[i][0]*(iからjまで行く通り数-i以降からjまで行く通り数)
				if(i<C){
					mint res=Bostan_Mori(C-i)*Bostan_Mori(N+j-C);
					res=Bostan_Mori(N+j-i)-res;
					for(int ii=i-1;ii>=0;ii--){
						S_sub[ii]+=res;
					}
					res-=S_sub[i];
					S[j]+=res*Q.table[i][0];
					continue;
				}
				mint res=Bostan_Mori(N+j-i);
				for(int ii=i-1;ii>=0;ii--){
					S_sub[ii]+=res;
				}
				res-=S_sub[i];
				S[j]+=res*Q.table[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];
		}
		ans=W-ans;
		printf("%d\n",ans.val());
	}
}
0