結果

問題 No.1516 simple 門松列 problem Re:MASTER
ユーザー 沙耶花沙耶花
提出日時 2021-05-21 22:25:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 524 ms / 6,000 ms
コード長 3,105 bytes
コンパイル時間 4,991 ms
コンパイル使用メモリ 267,236 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 15:57:40
合計ジャッジ時間 7,292 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 37 ms
6,940 KB
testcase_02 AC 224 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 10 ms
6,940 KB
testcase_07 AC 39 ms
6,940 KB
testcase_08 AC 56 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 12 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 446 ms
6,940 KB
testcase_15 AC 208 ms
6,940 KB
testcase_16 AC 99 ms
6,944 KB
testcase_17 AC 36 ms
6,944 KB
testcase_18 AC 11 ms
6,944 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 524 ms
6,940 KB
testcase_21 AC 508 ms
6,944 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 <typename T,typename F0,typename F1>
struct matrix{
	F0 func0;
	F1 func1;
	vector<vector<T>> value;
	int height,width;
	T init_value0,init_value1;
	
	matrix(vector<vector<T>> X,F0 f0,F1 f1,T iv0,T iv1):func0(f0),func1(f1){
		height = X.size();
		width = X[0].size();
		value = X;
		init_value0 = iv0,init_value1 = iv1;
	}
	
	matrix(int h,int w,F0 f0,F1 f1,T iv0,T iv1):func0(f0),func1(f1){
		vector<vector<T>> d(h,vector<T>(w,iv0));
		height = h,width = w;
		value = d;
		init_value0 = iv0,init_value1 = iv1;
	}
	
	
	void set_1(){
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				if(i==j)value[i][j] = init_value1;
				else value[i][j]=init_value0;
			}
		}
	}
	
	void show(){
		for(int i=0;i<height;i++){
			for(int j=0;j<width;j++){
				if(j!=0)cout<<' ';
				cout<<value[i][j];
			}
			cout<<endl;
		}
	}
	
	matrix &operator=(const matrix &another){
		value = another.value;
		height = another.height;
		width = another.width;
		return *this;
	}
	
	matrix &operator*=(const matrix &another){
		matrix<T,decltype(func0),decltype(func1)> R(height,another.width,func0,func1,init_value0,init_value1);
		for(int i=0;i<R.value.size();i++){
			for(int j=0;j<R.value[i].size();j++){
				R.value[i][j]=init_value0;
				for(int k=0;k<width;k++){
					R.value[i][j] = func0(R.value[i][j],func1(value[i][k],another.value[k][j]));
				}
			}
		}

		value = R.value;
		return (*this);
	}
	
	matrix operator*(const matrix &another)const{
		return (matrix(*this)*=another);
	}
	
	matrix beki(long long cnt){
		matrix<T,decltype(func0),decltype(func1)> R(height,width,func0,func1,init_value0,init_value1);
		R.set_1();
		auto temp = *this;
		while(cnt!=0LL){
			if(cnt%2==1){
				R *= temp;
			}
			temp *= temp;
			cnt/=2;
		}
		return R;
	}
};

int N,K;

pair<mint,mint> get(){
	
	vector<vector<mint>> A(K*K+K*K,vector<mint>(K*K+K*K,0));
	
	rep(i,K){
		rep(j,K){
			rep(k,K){
				if(i<j){
					if(k==i)continue;
					if(k>=j)continue;
					A[j*K+k][i*K+j] ++;
					A[K*K + j*K+k][K*K + i*K+j] ++;
					A[K*K + j*K+k][i*K+j] += k;
				}
				if(i>j){
					if(k==i)continue;
					if(k<=j)continue;
					A[j*K+k][i*K+j] ++;
					A[K*K + j*K+k][K*K + i*K+j] ++;
					A[K*K + j*K+k][i*K+j] += k;
				}
			}
		}
	}
	
	auto f0 = [](mint a,mint b){
		return a+b;
	};
	
	auto f1 = [](mint a,mint b){
		return a*b;
	};
	
	matrix<mint,decltype(f0),decltype(f1)> AA(A,f0,f1,0,1);
	
	AA = AA.beki(N-2);
	
	vector<vector<mint>> B(K*K+K*K,vector<mint>(1));
	
	rep(i,K){
		rep(j,K){
			if(i==j)continue;
			B[i*K+j][0] = 1;
			B[K*K + i*K+j][0] = i+j;
		}
	}
	
	matrix<mint,decltype(f0),decltype(f1)> BB(B,f0,f1,0,1);
	
	AA = AA*BB;
	
	pair<mint,mint> ret(0,0);
	
	rep(i,K*K){
		ret.first += AA.value[i][0];
		ret.second += AA.value[K*K + i][0];
	}
	
	return ret;
	
}

int main(){
	
	cin>>N>>K;
	
	pair<mint,mint> ret = get();
	
	cout<<ret.first.val()<<' '<<ret.second.val()<<endl;
			
	return 0;
}
0