結果

問題 No.823 Many Shifts Easy
ユーザー 👑 emthrmemthrm
提出日時 2019-04-26 22:21:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 2,508 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 125,960 KB
実行使用メモリ 159,420 KB
最終ジャッジ日時 2023-08-16 15:05:47
合計ジャッジ時間 5,969 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 372 ms
159,420 KB
testcase_01 AC 369 ms
159,340 KB
testcase_02 AC 371 ms
159,264 KB
testcase_03 AC 371 ms
159,228 KB
testcase_04 AC 369 ms
159,112 KB
testcase_05 AC 367 ms
159,268 KB
testcase_06 AC 374 ms
159,096 KB
testcase_07 AC 366 ms
159,116 KB
testcase_08 AC 369 ms
159,100 KB
testcase_09 AC 368 ms
159,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
struct Combinatorics {
  Combinatorics(int MAX = 5000000, long long mod_ = MOD) : mod(mod_) {
    MAX <<= 1;
    fact.resize(MAX + 1);
    fact_inv.resize(MAX + 1);
    fact[0] = 1;
    FOR(i, 1, MAX + 1) fact[i] = fact[i - 1] * i % mod;
    fact_inv[MAX] = mod_inv(fact[MAX]);
    for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i % mod;
  }

  inline long long nCk(int n, int k) {
    if (n < 0 || n < k || k < 0) return 0;
    return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod;
  }

  inline long long nPk(int n, int k) {
    if (n < 0 || n < k || k < 0) return 0;
    return fact[n] * fact_inv[n - k] % mod;
  }

  inline long long nHk(int n, int k) {
    if (n < 0 || k < 0) return 0;
    return (k == 0 ? 1 % mod : nCk(n + k - 1, k));
  }

private:
  long long mod;
  vector<long long> fact, fact_inv;

  long long mod_inv(long long a, long long mod = MOD) {
    a %= mod;
    if (__gcd(a, mod) != 1) return -1;
    long long b = mod, x = 1, y = 0;
    while (b > 0) {
      long long tmp = a / b;
      a -= tmp * b;
      swap(a, b);
      x -= tmp * y;
      swap(x, y);
    }
    x %= mod;
    if (x < 0) x += mod;
    return x;
  }
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, k; cin >> n >> k;
  Combinatorics com;
  long long nai = com.nPk(n - 1, k), kc2 = com.nCk(k, 2), nokori = com.nPk(n - 2, k - 2);
  long long ans = 0;
  FOR(i, 1, n + 1) {
    (ans += nai % MOD * i % MOD) %= MOD;
    if (i < n) {
      (ans += kc2 * nokori % MOD * i % MOD) %= MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}
0