結果

問題 No.2176 LRM Question 1
ユーザー nono00nono00
提出日時 2023-01-06 21:55:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 199,784 KB
実行使用メモリ 11,040 KB
最終ジャッジ日時 2023-08-20 15:02:04
合計ジャッジ時間 3,345 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 40 ms
11,040 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 19 ms
10,792 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 20 ms
10,756 KB
testcase_09 AC 20 ms
10,844 KB
testcase_10 AC 34 ms
9,656 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 42 ms
10,712 KB
testcase_15 AC 33 ms
9,204 KB
testcase_16 AC 28 ms
7,952 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 18 ms
6,140 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 16 ms
5,884 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

/*
 *
 * [L, R]を求めるが
 * M 以上のものは無視して良い
 * (n! = 0になる)
 *
 * fuct[i]: i!
 *
 * f(i, i) = 1!2!...i!
 *         
 *         = f(i - 1, i - 1) * i!
 *
 * f(l, r) = f(l, l)を求めた後、(l + 1)!をかけたものを足していけば良い
 *
 */

int main() {
    long long l, r, m;
    cin >> l >> r >> m;

    if (m <= l) {
        cout << 0 << endl;
        return 0;
    }
    if (m < r) {
        r = m;
    }

    vector<long long> fact(m + 1, 1);
    for (int i = 1; i <= m; i++) {
        fact[i] = i * fact[i - 1] % m;
    }

    auto f = [&](long long v) -> long long {
        long long result = 1;
        for (int i = 1; i <= v; i++) {
            result *= fact[i];
            result %= m;
        }

        return result;
    };

    long long now = f(l - 1);
    long long ans = 0;
    for (int i = l; i <= r; i++) {
        now *= fact[i];
        now %= m;
        ans += now;
        ans %= m;
    }

    cout << ans << endl;
}
0