結果

問題 No.1709 Indistinguishable by MEX
ユーザー 57tggx
提出日時 2021-10-15 21:45:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,928 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 71,156 KB
最終ジャッジ日時 2025-01-25 00:43:16
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdint>

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

int main(){
	std::size_t n;
	std::cin >> n;
	std::vector<std::size_t> pos(n);
	for(std::size_t i = 0; i < n; ++i){
		std::size_t p;
		std::cin >> p;
		pos[p] = i;
	}
	modint<998244353> ans = 1;
	std::size_t l = pos[0], r = pos[0];
	std::size_t tmp = 1;
	for(std::size_t i = 1; i < n; ++i){
		if(pos[i] > r){
			tmp += pos[i] - r;
			r = pos[i];
		}else if(pos[i] < l){
			tmp += l - pos[i];
			l = pos[i];
		}else{
			ans *= tmp - 1;
		}
		--tmp;
		// std::cout << i << ": " << l << " " << r << " (" << tmp << ")" << std::endl;
	}
	std::cout << ans.a << std::endl;
}
0