結果

問題 No.93 ペガサス
ユーザー koyumeishikoyumeishi
提出日時 2015-09-09 16:21:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 2,114 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 85,840 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 10:15:42
合計ジャッジ時間 3,061 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 32 ms
4,376 KB
testcase_05 AC 12 ms
4,376 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 109 ms
4,380 KB
testcase_08 AC 52 ms
4,376 KB
testcase_09 AC 195 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 164 ms
4,380 KB
testcase_13 AC 45 ms
4,380 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 144 ms
4,380 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 199 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

#define MOD 1000000007

void resize_(long long& vec, vector<int>::iterator begin_, vector<int>::iterator end_, const long long& val){
	vec = val;
	return;
}

template<class T, class U>
void resize_(vector<T>& vec, vector<int>::iterator begin_, vector<int>::iterator end_, const U& val){
	vec.resize(*begin_);
	for(int i=0; i<vec.size(); i++){
		resize_(vec[i], begin_+1, end_, val);
	}
}

template<class T, class U>
void resize_(vector<T>& vec, vector<int>&& sz, const U& val){
	resize_(vec, sz.begin(), sz.end(), val);
}


int main(){
	int n;
	cin >> n;

	if(n==1){
		cout << 1 << endl;
		return 0;
	}else if(n==2){
		cout << 2 << endl;
		return 0;
	}

	vector<vector<vector<long long>>> dp;
	resize_(dp, {n+2, 2, 2}, 0);
	dp[0][0][0] = 2;

	for(int i=2; i<n; i++){
		vector<vector<vector<long long>>> dp_;
		resize_(dp_, {n+2,2,2}, 0);

		for(int k=0; k<i; k++)
		for(int a=0; a<2; a++)
		for(int b=0; b<2; b++){
			if(a && b){
				if(k>0) dp_[k-1][b][0] += dp[k][a][b] * (k-2);
						dp_[k  ][b][1] += dp[k][a][b] * 1;
						dp_[k+1][b][1] += dp[k][a][b] * 1;
				if(k>0) dp_[k-1][0][0] += dp[k][a][b] * 1;
						dp_[k  ][b][0] += dp[k][a][b] * (i+1-1-k);

			}else if(a && !b){
				if(k>0) dp_[k-1][b][0] += dp[k][a][b] * (k-1);
						dp_[k  ][b][1] += dp[k][a][b] * 1;
						dp_[k+1][b][1] += dp[k][a][b] * 1;
						dp_[k  ][b][0] += dp[k][a][b] * (i+1-1-k);

			}else if(!a && b){
				if(k>0) dp_[k-1][b][0] += dp[k][a][b] * (k-1);
						dp_[k+1][b][1] += dp[k][a][b] * 2;
				if(k>0) dp_[k-1][0][0] += dp[k][a][b] * 1;
						dp_[k  ][b][0] += dp[k][a][b] * (i+1-2-k);

			}else if(!a && !b){
				if(k>0) dp_[k-1][b][0] += dp[k][a][b] * k;
						dp_[k+1][b][1] += dp[k][a][b] * 2;
						dp_[k  ][b][0] += dp[k][a][b] * (i+1-2-k);
			}
		}

		for(int k=0; k<=i; k++)
		for(int a=0; a<2; a++)
		for(int b=0; b<2; b++){
			dp_[k][a][b] %= MOD;
		}
		swap(dp, dp_);
	}

	cout << dp[0][0][0] << endl;
	return 0;
}
0