結果

問題 No.1709 Indistinguishable by MEX
ユーザー たこしたこし
提出日時 2021-10-15 23:28:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,878 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 202,248 KB
実行使用メモリ 8,420 KB
最終ジャッジ日時 2023-10-17 21:19:09
合計ジャッジ時間 4,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,752 KB
testcase_01 AC 3 ms
7,752 KB
testcase_02 AC 3 ms
7,752 KB
testcase_03 AC 3 ms
7,752 KB
testcase_04 AC 3 ms
7,752 KB
testcase_05 AC 3 ms
7,752 KB
testcase_06 AC 2 ms
7,752 KB
testcase_07 AC 3 ms
7,752 KB
testcase_08 AC 51 ms
8,420 KB
testcase_09 AC 42 ms
8,420 KB
testcase_10 AC 40 ms
8,420 KB
testcase_11 AC 32 ms
8,420 KB
testcase_12 AC 32 ms
8,420 KB
testcase_13 AC 47 ms
8,420 KB
testcase_14 AC 43 ms
8,420 KB
testcase_15 AC 46 ms
8,420 KB
testcase_16 AC 61 ms
8,420 KB
testcase_17 AC 55 ms
8,420 KB
testcase_18 AC 63 ms
8,420 KB
testcase_19 AC 63 ms
8,420 KB
testcase_20 AC 63 ms
8,420 KB
testcase_21 AC 63 ms
8,420 KB
testcase_22 AC 63 ms
8,420 KB
testcase_23 AC 62 ms
8,420 KB
testcase_24 AC 63 ms
8,420 KB
testcase_25 AC 62 ms
8,420 KB
testcase_26 AC 3 ms
7,752 KB
testcase_27 AC 34 ms
8,420 KB
testcase_28 AC 31 ms
8,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;

int N;
int P[MAX_N];

int memo[MAX_N];
int myPow(int k) {
    if (k <= 1) {
        return 1;
    }
    if (memo[k] != -1) {
        return memo[k];
    }
    memo[k] = k * myPow(k-1);
    memo[k] %= MOD9;
    return memo[k];
}

int I[MAX_N];

signed main() {
    memset(memo, -1, sizeof(memo));
    // cerr << (myPow(12) * 110) % MOD9 << endl;
    // exit(0);
    cin >> N;
    REP(n, N) {
        cin >> P[n];
    }

    REP(n, N) {
        I[P[n]] = n;
    }

    int ans = 1;
    int l = I[0], r = I[0];
    int dd = 0;
    FOR(k,1,N+1) {
        int next_l = min(l, I[k]);
        int next_r = max(r, I[k]);
        int diff = abs(l - next_l) + abs(r - next_r);
        if (1 <= diff) {
            dd += diff-1;
        }
        
        if (l < I[k] && I[k] < r) {
            ans *= dd;
            ans %= MOD9;
            dd--;
        }
        r = next_r; l = next_l;
    }

    ans *= myPow(dd);
    ans %= MOD9;


    cout << ans << endl;

    return 0;
}
0