結果

問題 No.2068 Restricted Permutation
ユーザー 遭難者遭難者
提出日時 2022-08-30 22:32:29
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,752 bytes
コンパイル時間 19,281 ms
コンパイル使用メモリ 247,020 KB
最終ジャッジ日時 2025-02-07 00:04:45
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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