結果

問題 No.140 みんなで旅行
ユーザー krotonkroton
提出日時 2015-01-29 19:53:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 572 ms / 5,000 ms
コード長 1,181 bytes
コンパイル時間 3,145 ms
コンパイル使用メモリ 145,704 KB
実行使用メモリ 9,240 KB
最終ジャッジ日時 2023-09-05 07:53:51
合計ジャッジ時間 16,243 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 548 ms
9,024 KB
testcase_01 AC 559 ms
8,984 KB
testcase_02 AC 562 ms
9,220 KB
testcase_03 AC 571 ms
8,980 KB
testcase_04 AC 572 ms
8,984 KB
testcase_05 AC 563 ms
8,968 KB
testcase_06 AC 555 ms
9,036 KB
testcase_07 AC 557 ms
8,968 KB
testcase_08 AC 547 ms
8,948 KB
testcase_09 AC 556 ms
9,136 KB
testcase_10 AC 563 ms
8,936 KB
testcase_11 AC 556 ms
9,224 KB
testcase_12 AC 554 ms
8,968 KB
testcase_13 AC 564 ms
9,228 KB
testcase_14 AC 557 ms
9,040 KB
testcase_15 AC 566 ms
9,036 KB
testcase_16 AC 559 ms
9,004 KB
testcase_17 AC 547 ms
8,952 KB
testcase_18 AC 548 ms
9,140 KB
testcase_19 AC 547 ms
8,972 KB
testcase_20 AC 547 ms
8,968 KB
testcase_21 AC 538 ms
9,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

ll dp[2][600][600];
ll ans[600];

void add(ll& a, ll b){
	a += b % MOD;
	a %= MOD;
}

void dp_solver(){
	const int n = 555;
	int curr = 0, next = 1;

	memset(dp[curr], 0 ,sizeof(dp[curr]));
	dp[curr][0][0] = 1;

	for(int use=0;use<n;use++){
		memset(dp[next], 0, sizeof(dp[next]));

		int rem = n - use;
		for(int ok=0;ok<=use;ok++){
			for(int ng=0;ng<=rem;ng++){
				ll now = dp[curr][ok][ng];
				if(now == 0)
					continue;

				add(dp[next][ok+1][ng], now);
				add(dp[next][ok][ng+2], now);

				if(ok > 0)
					add(dp[next][ok][ng], now * ok * ok);

				if(ng > 1)
					add(dp[next][ok][ng], now * ng * (ng - 1));

				if(ng > 0)
					add(dp[next][ok+1][ng-1], now * ng);

				if(ok > 0 && ng > 0)
					add(dp[next][ok][ng], now * ok * ng * 2);

				if(ok > 0)
					add(dp[next][ok][ng+1], now * ok * 2);

				if(ng > 0)
					add(dp[next][ok][ng+1], now * ng * 2);
			}
		}

		swap(curr, next);

		ll res = 0;
		for(int ok=0;ok<=use+1;ok++)
			add(res, dp[curr][ok][0]);

		ans[use+1] = res;
	}
}

int main(){
	dp_solver();

	int n; cin >> n;
	cout << ans[n] << endl;

	return 0;
}
0