結果

問題 No.1938 Lagrange Sum
ユーザー t33ft33f
提出日時 2022-05-13 23:33:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 840 ms / 3,000 ms
コード長 3,761 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 88,260 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 09:18:43
合計ジャッジ時間 13,242 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 840 ms
4,376 KB
testcase_01 AC 823 ms
4,376 KB
testcase_02 AC 835 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 537 ms
4,376 KB
testcase_06 AC 588 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 119 ms
4,376 KB
testcase_09 AC 37 ms
4,376 KB
testcase_10 AC 788 ms
4,376 KB
testcase_11 AC 132 ms
4,376 KB
testcase_12 AC 796 ms
4,376 KB
testcase_13 AC 805 ms
4,376 KB
testcase_14 AC 278 ms
4,376 KB
testcase_15 AC 22 ms
4,376 KB
testcase_16 AC 743 ms
4,376 KB
testcase_17 AC 263 ms
4,380 KB
testcase_18 AC 267 ms
4,376 KB
testcase_19 AC 764 ms
4,376 KB
testcase_20 AC 762 ms
4,380 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 747 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <numeric>
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;

template<int mod>
class modint {
    int val = 0;
    constexpr static int normalize(long long x) {
        if (0 <= x and x < mod) return x;
        else { x %= mod; return x >= 0 ? x : x + mod; }
    }
public:
    static const int modulus = mod;
    modint() {}
    constexpr modint(long long n) : val(normalize(n)) {}
    constexpr int value() const { return val; }
    constexpr modint operator-() const { return modint(mod - val); }
    constexpr modint inverse() const {
        long long x = mod, y = val, p = 1, q = 0, r = 0, s = 1;
        while (y != 0) {
            long long u = x / y;
            long long x0 = y; y = x - y * u; x = x0;
            long long r0 = p - r * u, s0 = q - s * u;
            p = r; r = r0; q = s; s = s0;
        }
        return modint(q);
    }
    constexpr const modint pow(long long e) const {
        if (e < 0) return pow(-e).inverse();
        long long ans = 1, p = val;
        while (e > 0) {
            if (e % 2 != 0) ans = (ans * p) % mod;
            p = (p * p) % mod;
            e >>= 1;
        }
        return modint(ans);
    }
    constexpr modint &operator+=(const modint r) {
        val += r.value();
        if (val >= mod) val -= mod;
        return *this;
    }
    constexpr modint &operator-=(const modint r) {
        val -= r.value();
        if (val < 0) val += mod;
        return *this;
    }
    constexpr modint &operator*=(const modint r) {
        val = (long long)val * r.value() % mod;
        return *this;
    }
    constexpr modint &operator/=(const modint r) {
        if (r.value() == 2) {
            val = (val % 2 ? val + mod : val) / 2;
        } else {
            val = (long long)val * r.inverse().value() % mod;
        }
        return *this;
    }

    friend constexpr modint operator+(const modint l, const modint r) {
        const int val = l.value() + r.value();
        return val >= mod ? val - mod : val;
    }
    friend constexpr modint operator-(const modint l, const modint r) { return l + (- r); }
    friend constexpr modint operator*(const modint l, const modint r) { return (long long)l.value() * r.value(); }
    friend constexpr modint operator/(const modint l, const modint r) { return l * r.inverse(); }
    friend constexpr bool operator==(const modint l, const modint r) { return l.value() == r.value(); }
    friend constexpr bool operator!=(const modint l, const modint r) { return l.value() != r.value(); }
};

constexpr int M = 998244353;
using mint = modint<M>;

int main() {
    int n, X; cin >> n >> X;
    vector<int> x(n), y(n);
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];

    for (int i = 0; i < n; i++) {
        if (x[i] == X) {
            mint ans = mint(n - 1) * y[i];
            for (int j = 0; j < n; j++)
                if (j != i) {
                    mint a = 1, b = 1;
                    for (int k = 0; k < n; k++) {
                        if (k != i && k != j) {
                            a *= x[i] - x[k];
                            b *= x[j] - x[k];
                        }
                    }
                    ans += a / b * y[j];
                }
            cout << ans.value() << '\n';
            return 0;
        }
    }

    mint ans = 0;
    vector<mint> z(n);
    for (int i = 0; i < n; i++) z[i] = mint(X - x[i]).inverse();
    for (int j = 0; j < n; j++) {
        mint a = 1, b = 0;
        for (int i = 0; i < n; i++) {
            if (i != j) {
                a *= x[j] - x[i];
                b += mint(x[j] - x[i]) * z[i];
            }
        }
        ans += b / a * y[j] * z[j];
    }
    for (int i = 0; i < n; i++) ans *= X - x[i];
    cout << ans.value() << '\n';
}
0