結果

問題 No.1392 Don't be together
ユーザー leaf_1415leaf_1415
提出日時 2021-02-12 22:10:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 720 ms / 2,000 ms
コード長 2,446 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 398,920 KB
最終ジャッジ日時 2023-09-27 04:23:01
合計ジャッジ時間 9,660 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,496 KB
testcase_01 AC 6 ms
9,624 KB
testcase_02 AC 6 ms
11,752 KB
testcase_03 AC 6 ms
9,552 KB
testcase_04 AC 6 ms
9,560 KB
testcase_05 AC 6 ms
9,704 KB
testcase_06 AC 720 ms
398,920 KB
testcase_07 AC 304 ms
204,344 KB
testcase_08 AC 551 ms
281,972 KB
testcase_09 AC 656 ms
334,172 KB
testcase_10 AC 362 ms
204,256 KB
testcase_11 AC 397 ms
204,464 KB
testcase_12 AC 373 ms
204,296 KB
testcase_13 AC 388 ms
204,492 KB
testcase_14 AC 358 ms
204,588 KB
testcase_15 AC 307 ms
204,256 KB
testcase_16 AC 430 ms
204,452 KB
testcase_17 AC 355 ms
204,316 KB
testcase_18 AC 393 ms
204,316 KB
testcase_19 AC 358 ms
204,280 KB
testcase_20 AC 117 ms
118,080 KB
testcase_21 AC 49 ms
73,016 KB
testcase_22 AC 260 ms
171,344 KB
testcase_23 AC 165 ms
134,652 KB
testcase_24 AC 117 ms
128,512 KB
testcase_25 AC 196 ms
142,656 KB
testcase_26 AC 55 ms
77,432 KB
testcase_27 AC 126 ms
114,092 KB
testcase_28 AC 170 ms
132,460 KB
testcase_29 AC 98 ms
97,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define outl(x) cout << x << endl
#define SP << " " << 
#define inf 1e18
#define mod 998244353

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

ll n, m;
ll p[5005];
ll dp[5005][5005];
ll dp2[5005][5005][2];
bool used[5005];
vector<ll> vec;

const int FACT_MAX = 200005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	make_fact();
	
	cin >> n >> m;
	rep(i, 1, n) cin >> p[i];
	
	if(m == 1){
		cout << 0 << endl;
		return 0;
	}
	
	rep(i, 1, n){
		if(used[i]) continue;
		ll v = i, cnt = 0;
		while(!used[v]){
			used[v] = true;
			cnt++;
			v = p[v];
		}
		vec.push_back(cnt);
	}
	
	dp[0][0] = 1;
	rep(i, 1, n) rep(j, 1, m){
		(dp[i][j] = dp[i-1][j-1] + j * dp[i-1][j] % mod) %= mod;
	}
	
	ll c = vec.size();
	dp2[0][0][0] = 1;
	rep(i, 0, c-1){
		rep(j, 0, n){
			rep(k, 0, 1){
				rep(l, 0, vec[i]-1){
					if(j+l <= n) (dp2[i+1][j+l][(k+l)&1] += comb(vec[i], l) * dp2[i][j][k] % mod) %= mod;
				}
				if(j+vec[i] <= n) (dp2[i+1][j+vec[i]-1][(k+vec[i])&1] += dp2[i][j][k]) %= mod;
			}
		}
	}
	
	ll ans = 0;
	rep(i, 0, n){
		ans += dp2[c][i][0] * dp[n-i][m] % mod, ans %= mod;
		ans += mod - dp2[c][i][1] * dp[n-i][m] % mod, ans %= mod;
	}
	cout << ans << endl;
	
	return 0;
}
0