結果

問題 No.1392 Don't be together
ユーザー SSRSSSRS
提出日時 2021-02-12 21:53:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 2,619 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 187,364 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 03:47:20
合計ジャッジ時間 4,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 127 ms
4,376 KB
testcase_07 AC 25 ms
4,376 KB
testcase_08 AC 125 ms
4,376 KB
testcase_09 AC 207 ms
4,380 KB
testcase_10 AC 73 ms
4,380 KB
testcase_11 AC 152 ms
4,376 KB
testcase_12 AC 67 ms
4,380 KB
testcase_13 AC 89 ms
4,376 KB
testcase_14 AC 97 ms
4,376 KB
testcase_15 AC 66 ms
4,380 KB
testcase_16 AC 138 ms
4,380 KB
testcase_17 AC 42 ms
4,380 KB
testcase_18 AC 90 ms
4,380 KB
testcase_19 AC 55 ms
4,376 KB
testcase_20 AC 59 ms
4,376 KB
testcase_21 AC 28 ms
4,376 KB
testcase_22 AC 63 ms
4,376 KB
testcase_23 AC 39 ms
4,380 KB
testcase_24 AC 25 ms
4,380 KB
testcase_25 AC 68 ms
4,380 KB
testcase_26 AC 27 ms
4,376 KB
testcase_27 AC 32 ms
4,380 KB
testcase_28 AC 61 ms
4,376 KB
testcase_29 AC 64 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
long long modinv(long long a){
	return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
vector<long long> mfi = {1};
long long modfact(int n){
	if (mf.size() > n){
		return mf[n];
	} else {
		for (int i = mf.size(); i <= n; i++){
			long long next = mf.back() * i % MOD;
			mf.push_back(next);
			mfi.push_back(modinv(next));
		}
		return mf[n];
	}
}
long long modfactinv(int n){
	if (mfi.size() > n){
		return mfi[n];
	} else {
		return modinv(modfact(n));
	}
}
long long modbinom(int n, int k){
	if (n < 0 || k < 0 || k > n){
		return 0;
	} else {
		return modfact(n) * modfactinv(k) % MOD * modfactinv(n - k) % MOD;
	}
}
vector<vector<long long>> matmul(vector<vector<long long>> A, vector<vector<long long>> B){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		for (int j = 0; j < N; j++){
			for (int k = 0; k < N; k++){
				ans[i][k] += A[i][j] * B[j][k] % MOD;
				ans[i][k] %= MOD;
			}
		}
	}
	return ans;
}
vector<vector<long long>> matexp(vector<vector<long long>> A, long long b){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		ans[i][i] = 1;
	}
	while (b > 0){
		if (b % 2 == 1){
			ans = matmul(ans, A);
		}
		A = matmul(A, A);
		b /= 2;
	}
	return ans;
}
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
    P[i]--;
  }
  vector<int> s;
  vector<bool> used(N, false);
  for (int i = 0; i < N; i++){
    if (!used[i]){
      int cnt = 0;
      for (int j = i; !used[j]; j = P[j]){
        used[j] = true;
        cnt++;
      }
      s.push_back(cnt);
    }
  }
  int cnt = s.size();
  long long ans = 0;
  for (int i = M; i >= 2; i--){
    long long add = 1;
    unordered_map<int, long long> mp;
    for (int j = 0; j < cnt; j++){
      if (!mp.count(s[j])){
        vector<vector<long long>> mat(2, vector<long long>(2));
        mat[0][0] = 0;
        mat[0][1] = i - 1;
        mat[1][0] = 1;
        mat[1][1] = i - 2;
        mat = matexp(mat, s[j]);
        mp[s[j]] = mat[0][0] * i % MOD;
      }
      add *= mp[s[j]];
      add %= MOD;
    }
    add *= modbinom(M, i);
    add %= MOD;
    if ((M - i) % 2 == 1){
      add = (MOD - add) % MOD;
    }
    ans += add;
    ans %= MOD;
  }
  ans *= modfactinv(M);
  ans %= MOD;
  cout << ans << endl;
}
0