結果

問題 No.2230 Good Omen of White Lotus
ユーザー みここ
提出日時 2023-02-24 22:27:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 3,429 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 80,488 KB
最終ジャッジ日時 2025-02-10 21:33:40
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template <int MOD>
struct static_modint
{
    using mint = static_modint;

public:
    int val;

    static_modint() : val(0) {}

    static_modint(long long v)
    {
        if (std::abs(v) >= mod())
        {
            v %= mod();
        }
        if (v < 0)
        {
            v += mod();
        }
        val = v;
    }

    mint &operator++()
    {
        val++;
        if (val == mod())
        {
            val = 0;
        }
        return *this;
    }

    mint &operator--()
    {
        if (val == 0)
        {
            val = mod();
        }
        val--;
        return *this;
    }

    mint &operator+=(const mint &x)
    {
        val += x.val;
        if (val >= mod())
        {
            val -= mod();
        }
        return *this;
    }

    mint &operator-=(const mint &x)
    {
        val -= x.val;
        if (val < 0)
        {
            val += mod();
        }
        return *this;
    }

    mint &operator*=(const mint &x)
    {
        val = (int)((long long)val * x.val % mod());
        return *this;
    }

    mint &operator/=(const mint &x)
    {
        *this *= x.inv();
        return *this;
    }

    mint operator-()
    {
        return mint() - *this;
    }

    mint pow(long long n) const
    {
        mint x = 1, r = *this;
        while (n)
        {
            if (n & 1)
            {
                x *= r;
            }
            r *= r;
            n >>= 1;
        }
        return x;
    }

    mint inv() const
    {
        return pow(mod() - 2);
    }

    friend mint operator+(const mint &x, const mint &y)
    {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y)
    {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y)
    {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y)
    {
        return mint(x) /= y;
    }

    friend bool operator==(const mint &x, const mint &y)
    {
        return x.val == y.val;
    }

    friend bool operator!=(const mint &x, const mint &y)
    {
        return x.val != y.val;
    }

    friend std::ostream &operator<<(std::ostream &os, const mint &x)
    {
        return os << x.val;
    }

    friend std::istream &operator>>(std::istream &is, mint &x)
    {
        long long v;
        is >> v;
        x = mint(v);
        return is;
    }

private:
    static constexpr int mod()
    {
        return MOD;
    }
};

using mint = static_modint<998244353>;

vector<int> v[200005];

int main()
{
    int h, w, n;
    mint p;
    cin >> h >> w >> n >> p;
    for(int i = 0; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        v[x].push_back(y);
    }
    vector<int> d;
    for(int x = 0; x < h; x++)
    {
        sort(v[x].begin(), v[x].end());
        for(int y : v[x])
        {
            int k = upper_bound(d.begin(), d.end(), y) - d.begin();
            if(k == d.size())
            {
                d.push_back(y);
            }
            else
            {
                d[k] = y;
            }
        }
    }
    int x = d.size();
    mint s = 1;
    for(int i = 0; i < x; i++)
    {
        s *= p - 2;
    }
    for(int i = x ; i < h + w - 3; i++)
    {
        s *= p - 1;
    }
    s /= p.pow(h + w - 3);
    cout << 1 - s << endl;
}
0