結果

問題 No.2810 Have Another Go (Hard)
ユーザー highlighterhighlighter
提出日時 2024-06-24 13:31:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 866 ms / 3,000 ms
コード長 5,301 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 85,316 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-24 14:08:04
合計ジャッジ時間 22,671 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
6,812 KB
testcase_01 AC 565 ms
6,944 KB
testcase_02 AC 362 ms
6,944 KB
testcase_03 AC 407 ms
6,940 KB
testcase_04 AC 141 ms
6,944 KB
testcase_05 AC 85 ms
6,940 KB
testcase_06 AC 132 ms
6,940 KB
testcase_07 AC 697 ms
6,940 KB
testcase_08 AC 69 ms
6,940 KB
testcase_09 AC 313 ms
6,940 KB
testcase_10 AC 847 ms
6,944 KB
testcase_11 AC 806 ms
6,940 KB
testcase_12 AC 849 ms
6,940 KB
testcase_13 AC 830 ms
6,940 KB
testcase_14 AC 822 ms
6,940 KB
testcase_15 AC 854 ms
6,940 KB
testcase_16 AC 821 ms
6,944 KB
testcase_17 AC 847 ms
6,940 KB
testcase_18 AC 825 ms
6,944 KB
testcase_19 AC 866 ms
6,948 KB
testcase_20 AC 816 ms
6,940 KB
testcase_21 AC 854 ms
6,940 KB
testcase_22 AC 812 ms
6,940 KB
testcase_23 AC 850 ms
6,940 KB
testcase_24 AC 848 ms
6,944 KB
testcase_25 AC 803 ms
6,940 KB
testcase_26 AC 846 ms
6,940 KB
testcase_27 AC 829 ms
6,944 KB
testcase_28 AC 843 ms
6,940 KB
testcase_29 AC 819 ms
6,940 KB
testcase_30 AC 5 ms
6,940 KB
testcase_31 AC 5 ms
6,940 KB
testcase_32 AC 4 ms
6,940 KB
testcase_33 AC 12 ms
6,940 KB
testcase_34 AC 7 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<vector>
#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;
	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/=2;
		}
		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 k;
	long long N,M;
	scanf("%lld%lld%d",&N,&M,&k);
	//1,1,2,4,8,16で始まり,前6項を足す線型漸化式に使うvectorを用意しておく
	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);
		vector<vector<mint>> P(6,vector<mint>(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++){
			vector<mint> P_sub(6);
			for(int i=5;i>=0;i--){
				if(i==C){
					P[i][j]=0;
					continue;
				}
				if(j==C){
					P[i][j]=0;
					continue;
				}
				if(i<C && j<C){
					P[i][j]=Bostan_Mori(C-i);
					P[i][j]*=Bostan_Mori(N+j-C);
					P[i][j]=Bostan_Mori(N+j-i)-P[i][j];
					if(i==0){
						R[j][i]=P[i][j];
					}
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=P[i][j];
					}
					P[i][j]-=P_sub[i];
					continue;
				}
				if(C<j && C<i){
					P[i][j]=Bostan_Mori(N+C-i);
					P[i][j]*=Bostan_Mori(j-C);
					P[i][j]=Bostan_Mori(N+j-i)-P[i][j];
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=P[i][j];
					}
					P[i][j]-=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);
					P[i][j]=res6-res1*res5-res4*res3+res1*res2*res3;
					if(i==0){
						R[j][i]=P[i][j];
					}
					for(int ii=i-1;ii>=0;ii--){
						P_sub[ii]+=P[i][j];
					}
					P[i][j]-=P_sub[i];
					continue;
				}
				P[i][j]=Bostan_Mori(N+j-i);
				for(int ii=i-1;ii>=0;ii--){
					P_sub[ii]+=P[i][j];
				}
				P[i][j]-=P_sub[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);
		Q*=R;
		vector<mint> S(6);
		for(int j=0;j<6;j++){
			vector<mint> S_sub(6);
			for(int i=5;i>=0;i--){
				if(Q[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[i][0];
					continue;
				}
				assert(i!=C);
				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[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