結果

問題 No.3044 よくあるカエルさん
ユーザー Nauclhlt🪷
提出日時 2025-01-16 19:53:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 6,328 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 202,296 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2025-01-18 00:35:12
合計ジャッジ時間 4,406 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define CONST_MOD 998244353L
// #define CONST_MOD 1000000007L
struct ModInt
{
    long long Value;

public:
    ModInt()
    {
        Value = 0L;
    }

    ModInt(long long value)
    {
        Value = value;
    }

    ModInt Power(long long exp) const
    {
        if (exp <= -1L)
        {
            return ModInt(1L) / Power(-exp);
        }
        if (exp == 0L)
            return 1L;
        if (exp == 1L)
            return *this;

        ModInt m = Power(exp / 2L);
        m = m * m;
        if (exp % 2L == 1L)
        {
            m = m * (*this);
        }

        return m;
    }

    ModInt Inv() const
    {
        return this->Power(CONST_MOD - 2L);
    }

    ModInt operator+() const
    {
        return *this;
    }

    ModInt operator-() const
    {
        return ModInt(-Value);
    }

    friend ModInt operator+(const ModInt& left, const ModInt& right)
    {
        return ModInt(SafeMod(left.Value + right.Value));
    }

    friend ModInt operator+(const ModInt& left, const long long& right)
    {
        return ModInt(SafeMod(left.Value + right));
    }

    friend ModInt operator+(const long long& left, const ModInt& right)
    {
        return ModInt(SafeMod(left + right.Value));
    }

    ModInt& operator+=(const ModInt& x)
    {
        Value += x.Value;
        Value = SafeMod(Value);

        return *this;
    }

    ModInt& operator+=(const long long& x)
    {
        Value += x;
        Value = SafeMod(Value);

        return *this;
    }

    friend ModInt operator-(const ModInt& left, const ModInt& right)
    {
        return ModInt(SafeMod(left.Value - right.Value));
    }

    friend ModInt operator-(const ModInt& left, const long long& right)
    {
        return ModInt(SafeMod(left.Value - right));
    }

    friend ModInt operator-(const long long& left, const ModInt& right)
    {
        return ModInt(SafeMod(left - right.Value));
    }

    ModInt& operator-=(const ModInt& x)
    {
        Value -= x.Value;
        Value = SafeMod(Value);

        return *this;
    }

    ModInt& operator-=(const long long& x)
    {
        Value -= x;
        Value = SafeMod(Value);

        return *this;
    }

    friend ModInt operator*(const ModInt& left, const ModInt& right)
    {
        return ModInt(SafeMod(left.Value * right.Value));
    }

    friend ModInt operator*(const ModInt& left, const long long& right)
    {
        return ModInt(SafeMod(left.Value * right));
    }

    friend ModInt operator*(const long long& left, const ModInt& right)
    {
        return ModInt(SafeMod(left * right.Value));
    }

    ModInt& operator*=(const ModInt& x)
    {
        Value *= x.Value;
        Value = SafeMod(Value);

        return *this;
    }

    ModInt& operator*=(const long long& x)
    {
        Value *= x;
        Value = SafeMod(Value);

        return *this;
    }

    friend ModInt operator /(const ModInt& left, const ModInt& right)
    {
        ModInt inv = right.Inv();
        return ModInt(SafeMod(left.Value * inv.Value));
    }

    friend ModInt operator/(const ModInt& left, const long long& right)
    {
        return ModInt(SafeMod(left.Value * ModInt(right).Inv().Value));
    }

    friend ModInt operator/(const long long& left, const ModInt& right)
    {
        return ModInt(SafeMod(left * right.Inv().Value));
    }

    ModInt& operator/=(const ModInt& x)
    {
        Value *= x.Inv().Value;
        Value = SafeMod(Value);

        return *this;
    }

    ModInt& operator/=(const long long& x)
    {
        Value *= ModInt(x).Inv().Value;
        Value = SafeMod(Value);

        return *this;
    }

    ModInt& operator++()
    {
        ++Value;
        Value = SafeMod(Value);
        return *this;
    }

    ModInt operator++(int)
    {
        ModInt temp = *this;
        Value++;
        Value = SafeMod(Value);
        return temp;
    }

    ModInt& operator--()
    {
        --Value;
        Value = SafeMod(Value);
        return *this;
    }

    ModInt operator--(int)
    {
        ModInt temp = *this;
        Value--;
        Value = SafeMod(Value);
        return temp;
    }

    inline static ModInt One()
    {
        return ModInt(1L);
    }

    static ModInt Combination(long long n, long long r)
    {
        ModInt c = 1L;
        for (ModInt i = 1; i.Value <= r; i++)
        {
            c = c * (ModInt(n) - i + ModInt::One()) / i;
        }
        return c;
    }

private:
    inline static long long SafeMod(long long a)
    {
        a %= CONST_MOD;
        if (a < 0)
        {
            a += CONST_MOD;
        }
        return a;
    }
};

int main()
{
    ll N;
    cin >> N;
    int T;
    cin >> T;
    int k, l;
    cin >> k >> l;


    auto mult_matrix = [&](vector<vector<ModInt>>& a, vector<vector<ModInt>>& b)
    {
        int size = a.size();

        vector<vector<ModInt>> res(size, vector<ModInt>(size, 0L));

        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                for (int k = 0; k < size; k++)
                {
                    res[i][j] += b[k][j] * a[i][k];
                }
            }
        }

        return res;
    };

    auto matrix_pow = [&](auto self, vector<vector<ModInt>>& m, ll exp)
    {
        if (exp == 1)
            return m;
        if (exp == 0)
        {
            vector<vector<ModInt>> id(m.size(), vector<ModInt>(m.size()));
            for (int i = 0; i < id.size(); i++)
            {
                id[i][i] = 1LL;
            }

            return id;
        }

        vector<vector<ModInt>> half = self(self, m, exp / 2LL);
        vector<vector<ModInt>> p = mult_matrix(half, half);
        if (exp % 2 != 0)
            p = mult_matrix(m, p);

        return p;
    };

    vector<vector<ModInt>> rec(T, vector<ModInt>(T));
    for (int i = 0; i < T - 1; i++)
    {
        rec[i][i + 1] = 1;
    }

    ModInt one = ModInt(k - 1) / ModInt(6LL);
    ModInt two = ModInt(l - k) / ModInt(6LL);
    ModInt moveT = ModInt(7 - l) / ModInt(6LL);

    rec[T - 1][T - 1] = one;
    rec[T - 1][T - 2] = two;
    rec[T - 1][0] = moveT;

    vector<vector<ModInt>> f = matrix_pow(matrix_pow, rec, N - 1);

    ModInt ans = f[T - 1][T - 1];
    cout << ans.Value << endl;
}
0