結果

問題 No.2474 Empty Quartz
ユーザー だれだれ
提出日時 2023-08-16 19:45:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 819 ms
コンパイル使用メモリ 81,548 KB
実行使用メモリ 5,052 KB
最終ジャッジ日時 2023-09-02 16:16:10
合計ジャッジ時間 1,883 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,864 KB
testcase_01 AC 4 ms
4,944 KB
testcase_02 AC 25 ms
5,052 KB
testcase_03 AC 33 ms
4,980 KB
testcase_04 AC 33 ms
5,044 KB
testcase_05 AC 36 ms
4,960 KB
testcase_06 AC 36 ms
4,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/modint>
#include <cmath>
#include <iostream>
using namespace std;

using mint = atcoder::modint998244353;
using i64 = long long;

mint fact[200010];
mint ifact[200010];

mint binom(int n, int r) {
    if (n < 0 || r < 0 || n < r) return 0;
    return fact[n] * ifact[r] * ifact[n - r];
}

void solve() {
    i64 n, k;
    cin >> n >> k;
    i64 d = (n + 1) * (n + 1) - 4 * k;
    if (d < 0) {
        cout << 0 << "\n";
        return;
    }
    i64 tmpr = sqrtl(d);
    i64 r = -1;
    for (i64 j = max(0ll, tmpr - 3); j <= tmpr + 3; j++) {
        if (j * j == d) {
            r = j;
            break;
        }
    }
    if (r == -1) {
        cout << 0 << "\n";
        return;
    }
    i64 a = n + 1 + r;
    if (a < 0 && a & 1) {
        cout << 0 << "\n";
        return;
    }
    a >>= 1;
    i64 b = n + 1 - a;
    if (b < 0) {
        cout << 0 << "\n";
        return;
    }
    mint ans = binom(a + b - 1, a) + binom(a + b - 1, a - 1);
    if (a == b) ans -= binom(a + b - 1, a);
    cout << ans.val() << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    fact[0] = 1;
    for (int i = 1; i <= 200000; i++) {
        fact[i] = fact[i - 1] * i;
    }
    ifact[200000] = fact[200000].inv();
    for (int i = 200000; i > 0; i--) {
        ifact[i - 1] = ifact[i] * i;
    }
    while (t--) solve();
}
0