結果

問題 No.1709 Indistinguishable by MEX
ユーザー たこしたこし
提出日時 2021-10-15 23:28:48
言語 C++17
(gcc 12.2.0 + boost 1.81.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,878 bytes
コンパイル時間 1,835 ms
コンパイル使用メモリ 199,080 KB
実行使用メモリ 8,140 KB
最終ジャッジ日時 2023-07-16 23:28:18
合計ジャッジ時間 4,456 ms
ジャッジサーバーID
(参考情報)
judge17 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,876 KB
testcase_01 AC 2 ms
5,904 KB
testcase_02 AC 2 ms
6,096 KB
testcase_03 AC 2 ms
5,888 KB
testcase_04 AC 2 ms
5,940 KB
testcase_05 AC 3 ms
5,940 KB
testcase_06 AC 3 ms
5,872 KB
testcase_07 AC 2 ms
5,888 KB
testcase_08 AC 43 ms
7,540 KB
testcase_09 AC 37 ms
7,080 KB
testcase_10 AC 35 ms
6,996 KB
testcase_11 AC 30 ms
6,756 KB
testcase_12 AC 29 ms
6,712 KB
testcase_13 AC 43 ms
7,324 KB
testcase_14 AC 38 ms
7,164 KB
testcase_15 AC 39 ms
7,408 KB
testcase_16 AC 53 ms
7,992 KB
testcase_17 AC 50 ms
7,700 KB
testcase_18 AC 58 ms
8,080 KB
testcase_19 AC 55 ms
8,048 KB
testcase_20 AC 56 ms
8,020 KB
testcase_21 AC 57 ms
8,020 KB
testcase_22 AC 56 ms
8,088 KB
testcase_23 AC 56 ms
8,032 KB
testcase_24 AC 56 ms
8,140 KB
testcase_25 AC 56 ms
8,052 KB
testcase_26 AC 2 ms
5,888 KB
testcase_27 AC 30 ms
6,748 KB
testcase_28 AC 28 ms
6,800 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