結果

問題 No.824 Many Shifts Hard
ユーザー gazellegazelle
提出日時 2019-04-26 22:56:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 466 ms / 2,000 ms
コード長 1,791 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 121,188 KB
最終ジャッジ日時 2025-01-07 03:14:43
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<unordered_map>
#include<stack>
#include<queue>
#include<math.h>
#include<functional>
#include<bitset>
#include<cassert>
using namespace std;
using lint = long long;
using ld = long double;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i,n,m) for(lint i=n;i<(int)m;i++)
#define REP(i,n) FOR(i,0,n)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ALL(v) v.begin(),v.end()
#define UNIQUE(v)  sort(ALL(v));v.erase(unique(ALL(v)),v.end());
#define pb push_back

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	lint n, k;
	cin >> n >> k;
	if(n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<vector<vector<lint>>> dp(k + 1, vector<vector<lint>>(k + 10, vector<lint>(k + 10, 0)));
	REP(i, k + 1) REP(j, k + 10) REP(l, k + 10) {
		if(j >= l) continue;
		if(i == 0) {
			if(j == 0 && l == 1) dp[i][j][l] = 1;
		} else {
			if(j == 0) {
				dp[i][j][l] = (n - 1) * dp[i - 1][j][l]; dp[i][j][l] %= MOD;
				if(l > 1) dp[i][j][l] += dp[i - 1][j][l - 1]; dp[i][j][l] %= MOD;
			} else {
				dp[i][j][l] = (n - 2) * dp[i - 1][j][l]; dp[i][j][l] %= MOD;
				if(l - j > 1) {
					dp[i][j][l] += dp[i - 1][j][l - 1]; dp[i][j][l] %= MOD;
				}
				if(j) {
					dp[i][j][l] += dp[i - 1][j - 1][l]; dp[i][j][l] %= MOD;
				} else {
					dp[i][j][l] += dp[i - 1][j][l]; dp[i][j][l] %= MOD;
				}
			}
		}
	}
	lint ans = 0;
	FOR(i, 1, n + 1) {
		for(lint j = 0; j <= k; j++) {
			if(i + j <= n) {
				ans += i * dp[k][1 + j - 1][1 + j];
				ans %= MOD;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
/* --------------------------------------- */
0