結果

問題 No.2068 Restricted Permutation
ユーザー 遭難者遭難者
提出日時 2022-08-30 22:32:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,752 bytes
コンパイル時間 5,370 ms
コンパイル使用メモリ 220,156 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-27 04:14:36
合計ジャッジ時間 6,234 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 17 ms
6,940 KB
testcase_17 AC 20 ms
6,940 KB
testcase_18 AC 5 ms
6,940 KB
testcase_19 AC 35 ms
6,944 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 35 ms
6,940 KB
testcase_22 AC 5 ms
6,940 KB
testcase_23 AC 20 ms
6,940 KB
testcase_24 AC 19 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <string>
#include <stack>
#include <numeric>
#include <algorithm>
using namespace std;
using ll = long long;
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
void chmin(ll &a, ll b)
{
    if (a > b)
        a = b;
}
void chmax(ll &a, ll b)
{
    if (a < b)
        a = b;
}
vector<int> z(int n, vector<int> &a)
{
    vector<int> b = a;
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    vector<int> res(n);
    rep(i, n) res[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    return res;
}
int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, k, x;
    cin >> n >> k >> x;
    vector<mint> fa(n + 1);
    fa[0] = 1;
    for (int i = 1; i <= n; i++)
        fa[i] = i * fa[i - 1];
    vector<mint> fa_inv(n + 1);
    fa_inv[n] = 1 / fa[n];
    for (int i = n - 1; i >= 0; i--)
        fa_inv[i] = (i + 1) * fa_inv[i + 1];
    mint ans = (fa[n - k] - 1) / 2;
    for (int i = 1; i < k; i++)
    {
        mint tmp = fa[n - i + 1] * (n - i) / 2;
        tmp -= fa[n - i] * (x - 1);
        tmp += (x - 1) * fa[n - i] * (i - 1) * fa[n - 2] * fa_inv[n - 1];
        ans += tmp * fa[n - i - 1] * fa_inv[n - i];
    }
    ans += fa[n - k] * (x - 1);
    ans -= (x - 1) * fa[n - k] * (k - 1) * fa[n - 2] * fa_inv[n - 1];
    ans *= fa[n - 1];
    cout << ans.val() << endl;
    return 0;
}
0