結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー sten_sansten_san
提出日時 2021-01-23 00:08:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 677 ms / 2,000 ms
コード長 2,292 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 203,692 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 09:10:15
合計ジャッジ時間 6,911 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 650 ms
4,376 KB
testcase_12 AC 537 ms
4,376 KB
testcase_13 AC 677 ms
4,376 KB
testcase_14 AC 530 ms
4,376 KB
testcase_15 AC 557 ms
4,376 KB
testcase_16 AC 547 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

tuple<int64_t, int64_t, int64_t> exgcd(int64_t a, int64_t b) {
    if (b != 0) {
        auto [xp, yp, gcd] = exgcd(b, a % b);
        return { yp, xp - a / b * yp, gcd };
    }
    else {
        return { 1, 0, a };
    }
}

int main() {
    constexpr int64_t mod = 1e9 + 7;

    int t; cin >> t;
    while (t--) {
        int64_t n, k, h, y; cin >> n >> k >> h >> y;
        tie(n, k, h) = make_tuple(
            min({ n, k, h }),
            n + k + h - min({ n, k, h }) - max({ n, k, h }),
            max({ n, k, h })
        );

        int64_t ans = 0;

        auto [n1, n2, gcd] = exgcd(n, k);
        auto np = n / gcd, kp = k / gcd;
        for (int i = 0; i * h <= y; ++i) {
            auto c = y - i * h;

            if (c == 0) {
                ++ans;
                ans %= mod;
            }
            else {
                ans += c % n == 0;
                ans %= mod;
                ans += c % k == 0;
                ans %= mod;
            }

            if (c % gcd) continue;
            auto a = n1 * (c / gcd), b = n2 * (c / gcd);

            auto X = [&](auto n) { return a - kp * n; };
            auto Y = [&](auto n) { return b + np * n; };

            int64_t xmax = 0, ymin = 0;
            if (a == 0) {
                xmax = -1;
            }
            if (0 < a) {
                xmax = a / kp - (a % kp == 0);
            }
            if (a < 0) {
                xmax = a / kp - 1;
            }

            if (b == 0) {
                ymin = 1;
            }
            if (0 < b) {
                ymin = -b / np + (b % np == 0);
            }
            if (b < 0) {
                ymin = -b / np + 1;
            }

            if (ymin <= xmax) {
                ans += xmax - ymin + 1;
                ans %= mod;
            }
        }

        cout << ans << endl;
    }
}

0