結果

問題 No.1938 Lagrange Sum
ユーザー sotanishysotanishy
提出日時 2022-05-13 22:49:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,726 ms / 3,000 ms
コード長 3,297 bytes
コンパイル時間 2,117 ms
コンパイル使用メモリ 203,824 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 08:27:09
合計ジャッジ時間 25,713 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,726 ms
4,376 KB
testcase_01 AC 1,726 ms
4,376 KB
testcase_02 AC 1,724 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1,078 ms
4,376 KB
testcase_06 AC 1,180 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 235 ms
4,380 KB
testcase_09 AC 71 ms
4,380 KB
testcase_10 AC 1,588 ms
4,376 KB
testcase_11 AC 260 ms
4,376 KB
testcase_12 AC 1,606 ms
4,376 KB
testcase_13 AC 1,620 ms
4,380 KB
testcase_14 AC 553 ms
4,376 KB
testcase_15 AC 42 ms
4,380 KB
testcase_16 AC 1,498 ms
4,376 KB
testcase_17 AC 522 ms
4,376 KB
testcase_18 AC 530 ms
4,376 KB
testcase_19 AC 1,539 ms
4,376 KB
testcase_20 AC 1,534 ms
4,376 KB
testcase_21 AC 19 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 14 ms
4,380 KB
testcase_24 AC 1,501 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <int mod>
class Modint {
    using mint = Modint;
    static_assert(mod > 0, "Modulus must be positive");

public:
    static constexpr int get_mod() noexcept { return mod; }

    constexpr Modint(long long y = 0) noexcept : x(y >= 0 ? y % mod : (y % mod + mod) % mod) {}

    constexpr int value() const noexcept { return x; }

    constexpr mint& operator+=(const mint& r) noexcept { if ((x += r.x) >= mod) x -= mod; return *this; }
    constexpr mint& operator-=(const mint& r) noexcept { if ((x += mod - r.x) >= mod) x -= mod; return *this; }
    constexpr mint& operator*=(const mint& r) noexcept { x = static_cast<int>(1LL * x * r.x % mod); return *this; }
    constexpr mint& operator/=(const mint& r) noexcept { *this *= r.inv(); return *this; }

    constexpr mint operator-() const noexcept { return mint(-x); }

    constexpr mint operator+(const mint& r) const noexcept { return mint(*this) += r; }
    constexpr mint operator-(const mint& r) const noexcept { return mint(*this) -= r; }
    constexpr mint operator*(const mint& r) const noexcept { return mint(*this) *= r; }
    constexpr mint operator/(const mint& r) const noexcept { return mint(*this) /= r; }

    constexpr bool operator==(const mint& r) const noexcept { return x == r.x; }
    constexpr bool operator!=(const mint& r) const noexcept { return x != r.x; }

    constexpr mint inv() const noexcept {
        int a = x, b = mod, u = 1, v = 0;
        while (b > 0) {
            int t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return mint(u);
    }

    constexpr mint pow(long long n) const noexcept {
        mint ret(1), mul(x);
        while (n > 0) {
            if (n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend std::ostream& operator<<(std::ostream& os, const mint& r) {
        return os << r.x;
    }

    friend std::istream& operator>>(std::istream& is, mint& r) {
        long long t;
        is >> t;
        r = mint(t);
        return is;
    }

private:
    int x;
};

using mint = Modint<998244353>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    mint X;
    cin >> N >> X;
    vector<mint> x(N), y(N);
    rep(i,0,N) cin >> x[i] >> y[i];

    vector<mint> prodinv(N, 1);
    rep(i,0,N) {
        mint p = 1;
        rep(k,0,N) if (k != i) p *= x[i] - x[k];
        prodinv[i] = mint(1)/p;
    }

    mint ans = 0;
    rep(i,0,N) {
        mint left = 1;
        rep(k,0,i) left *= X - x[k];
        vector<mint> right(N, 1);
        revrep(k,N-1,i) right[k] = right[k+1] * (X - x[k+1]);
        rep(j,i+1,N) {
            ans += (x[j] - x[i]) * (y[j] * prodinv[j] - y[i] * prodinv[i]) * left * right[j];
            left *= X - x[j];
        }
    }
    cout << ans << endl;
}
0