結果

問題 No.1489 Repeat Cumulative Sum
ユーザー t33f
提出日時 2021-04-23 23:40:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 614 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 669 ms
コンパイル使用メモリ 67,200 KB
最終ジャッジ日時 2025-01-21 00:29:20
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

constexpr int M = 1000000007;

long long modinv(long long a, long long m) {
    long long x = m, y = a, 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 q < 0 ? q + m : q;
}

int main() {
    long long n, m; cin >> n >> m;
    int a[n-1]; for (int i = 0; i < n-1; i++) cin >> a[i];
    long long ans = 0, c = m % M;
    for (int i = n-2; i >= 0; i--) {
        ans += a[i] * c % M;
        cerr << ans << ' ' << c <<  endl;
        c = (n + m - i - 1) % M * modinv(n - i, M) % M * c % M;
    }
    cout << ans % M << endl;
}
0