結果

問題 No.1186 長方形の敷き詰め
ユーザー WarToksWarToks
提出日時 2020-08-22 14:55:06
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 4,994 bytes
コンパイル時間 1,343 ms
コンパイル使用メモリ 95,212 KB
実行使用メモリ 237,580 KB
最終ジャッジ日時 2023-08-05 12:10:44
合計ジャッジ時間 11,929 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 378 ms
237,324 KB
testcase_01 AC 379 ms
237,296 KB
testcase_02 AC 377 ms
237,448 KB
testcase_03 AC 374 ms
237,484 KB
testcase_04 AC 371 ms
237,472 KB
testcase_05 AC 376 ms
237,480 KB
testcase_06 AC 374 ms
237,308 KB
testcase_07 AC 381 ms
237,216 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 379 ms
237,564 KB
testcase_12 AC 373 ms
237,460 KB
testcase_13 AC 373 ms
237,580 KB
testcase_14 AC 375 ms
237,264 KB
testcase_15 AC 374 ms
237,288 KB
testcase_16 AC 379 ms
237,268 KB
testcase_17 AC 372 ms
237,516 KB
testcase_18 AC 373 ms
237,420 KB
testcase_19 AC 377 ms
237,520 KB
testcase_20 AC 376 ms
237,520 KB
testcase_21 AC 379 ms
237,296 KB
testcase_22 AC 382 ms
237,336 KB
testcase_23 AC 377 ms
237,512 KB
testcase_24 AC 376 ms
237,296 KB
testcase_25 AC 372 ms
237,336 KB
testcase_26 AC 373 ms
237,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <utility>
#include <vector>

// modint構造体
template <int modulus> struct mod_Int{
    int val; // 値本体
    // コンストラクタ
    constexpr mod_Int():val(0){}
    constexpr mod_Int(const int& v):val(v % modulus){ if(val < 0) val += modulus; }
    constexpr mod_Int(int&& v):val(v % modulus){ if(val < 0) val += modulus; }
    constexpr mod_Int(const long long int& v):val(v % modulus){ if(val < 0) val += modulus; }
    constexpr mod_Int(long long int&& v):val(v % modulus){ if(val < 0) val += modulus; }
    constexpr mod_Int(const unsigned int& v):val(v % modulus){}
    constexpr mod_Int(unsigned int&& v):val(v % modulus){}
    constexpr mod_Int(const unsigned long long int& v):val(v % modulus){}
    constexpr mod_Int(unsigned long long int&& v):val(v % modulus){}
    constexpr mod_Int& operator += (const mod_Int<modulus>& other) noexcept {
        val += other.val; if(val >= modulus) val -= modulus;
        return *this;
    }
    constexpr mod_Int& operator -= (const mod_Int<modulus>& other) noexcept {
        val -= other.val; if(val < 0) val += modulus;
        return *this;
    }
    constexpr mod_Int& operator *= (const mod_Int<modulus>& other) noexcept {
        val = static_cast<long long int>(val) * other.val % modulus;
        return *this; 
    }
    constexpr mod_Int& operator /= (const mod_Int<modulus>& other) noexcept {
        val = static_cast<long long int>(val) * other.inverse() % modulus;
        return *this;
    }
    constexpr bool operator == (const mod_Int<modulus>& other) const noexcept {
        return val == other.val;
    }
    constexpr mod_Int operator +(const mod_Int<modulus>& v) const noexcept{
        return mod_Int<modulus>(*this) += v;
    }
    constexpr mod_Int operator -(const mod_Int<modulus>& v)const noexcept{
        return mod_Int<modulus>(*this) -= v;
    }
    constexpr mod_Int operator *(const mod_Int<modulus>& v) const noexcept{
        return mod_Int<modulus>(*this) *= v;
    }
    constexpr mod_Int operator /(const mod_Int<modulus>& v) const noexcept{
        return mod_Int<modulus>(*this) /= v;
    }
    constexpr mod_Int& operator ++(void) noexcept {
        if(++val == modulus) val = 0;
        return *this;
    }
    constexpr mod_Int& operator --(void) noexcept {
        if(val-- == 0) val = modulus - 1;
        return *this;
    }
    constexpr mod_Int operator -()const noexcept{
        return mod_Int<modulus>((val == 0 ? 0 : modulus - val));
    }

    // aの逆元を求める関数
    static constexpr int inverse(int a) noexcept {
        int b = modulus, 
            u = 1, v = 0;
        while (b != 0) {
            const int t = a / b; 
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        if(u < 0) u += modulus;
        return u;
    }
    constexpr int inverse(void) const noexcept { return inverse(val); }
    // a^nを返す関数 : nは負の数でも可
    static constexpr int power(int a, long long int n){
        long long res = 1, waiting = a;
        if(n < 0) waiting = inverse(a), n = -n;
        while(n != 0){ 
            if(n % 2 != 0) res = res * waiting % modulus; 
            waiting = waiting * waiting % modulus; 
            n /= 2;
        }
        return res;
    }
    constexpr mod_Int power(long long int n) const noexcept { mod_Int res; res.val = power(val, n); return res; }
    friend std::ostream& operator << (std::ostream& os_arg, const mod_Int<modulus>& mod_Int_arg){
        os_arg << mod_Int_arg.val;
        return os_arg;
    }
};

template <int modulus>
struct Combination{
    const int sz;
    std::vector<mod_Int<modulus>> Power, Inv_Power, Inv;
    
    // コンストラクタ : n は想定される最大値
    Combination(const int n):sz(n){
        Power.resize(n + 1); Power[0].val = 1;
        mod_Int<modulus> v = 1; for(int i = 1; i <= n; ++i, ++v) Power[i] = Power[i - 1] * v;
        Inv_Power.resize(n + 1); Inv_Power[n] = Power[n].inverse();
        v = n; for(int i = n; i > 0; --i, --v) Inv_Power[i - 1] = Inv_Power[i] * v;
        Inv.resize(n + 1);
        Inv[0] = 0; for(int i = 1; i <= n; ++i) Inv[i] = Inv_Power[i] * Power[i - 1];
    }
    
    // 組み合わせ nCk を求める関数
    mod_Int<modulus> combination(const int n, const int k) const {
        if(k < 0 or n < k) return mod_Int<modulus>();
        return Power[n] * Inv_Power[k] * Inv_Power[n - k];
    }
};

constexpr int mod = 998244353;
using modint = mod_Int<mod>;

int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); 
    std::cout << std::fixed << std::setprecision(16);
    int n, m; std::cin >> n >> m;
    if(n == 1){
        std::cout << "1\n"; return 0;
    }
    Combination<mod> C(20'000'000);
    modint res;
    for(int k = 0, r = m; r >= 0; ++k, r -= n) res += C.combination(r + k, k);
    std::cout << res << '\n';
    
    return 0;
}
0