結果

問題 No.1035 Color Box
コンテスト
ユーザー Dashcoding
提出日時 2026-03-27 21:35:59
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,598 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,175 ms
コンパイル使用メモリ 162,652 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-27 21:37:24
合計ジャッジ時間 6,618 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

const int MOD = 1e9 + 7;
const int MAX = 1e5 + 10;

long long fact[MAX];       // 阶乘数组
long long inv_fact[MAX];   // 阶乘的逆元数组

// 快速幂计算 (a^b) mod MOD
long long powmod(long long a, long long b) {
    long long res = 1;
    a %= MOD;
    while (b > 0) {
        if (b % 2 == 1) {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b /= 2;
    }
    return res;
}

// 预处理阶乘和逆元阶乘
void precompute() {
    fact[0] = 1;
    for (int i = 1; i < MAX; ++i) {
        fact[i] = fact[i-1] * i % MOD;
    }
    // 费马小定理求逆元:inv_fact[MAX-1] = fact[MAX-1]^(MOD-2) mod MOD
    inv_fact[MAX-1] = powmod(fact[MAX-1], MOD-2);
    for (int i = MAX-2; i >= 0; --i) {
        inv_fact[i] = inv_fact[i+1] * (i+1LL) % MOD;
    }
}

// 计算组合数 C(m, k) = m!/(k!*(m-k)!) mod MOD
long long comb(int m, int k) {
    if (k < 0 || k > m) return 0;
    return fact[m] * inv_fact[k] % MOD * inv_fact[m - k] % MOD;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    precompute();
    
    int N, M;
    cin >> N >> M;
    
    long long ans = 0;
    for (int k = 0; k <= M; ++k) {
        long long c = comb(M, k);
        // 符号项:(-1)^k 等价于 MOD-1 当k为奇数,1当k为偶数
        long long sign = (k % 2 == 0) ? 1 : MOD - 1;
        long long p = powmod(M - k, N);
        long long term = sign * c % MOD;
        term = term * p % MOD;
        ans = (ans + term) % MOD;
    }
    
    cout << ans << '\n';
    return 0;
}
0