結果

問題 No.823 Many Shifts Easy
ユーザー 👑 jupirojupiro
提出日時 2020-01-21 20:09:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 2,530 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 113,936 KB
実行使用メモリ 123,084 KB
最終ジャッジ日時 2023-09-20 08:28:37
合計ジャッジ時間 5,217 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 304 ms
123,032 KB
testcase_01 AC 318 ms
123,000 KB
testcase_02 AC 310 ms
123,056 KB
testcase_03 AC 307 ms
122,936 KB
testcase_04 AC 311 ms
122,956 KB
testcase_05 AC 313 ms
123,020 KB
testcase_06 AC 304 ms
123,084 KB
testcase_07 AC 311 ms
122,932 KB
testcase_08 AC 311 ms
123,052 KB
testcase_09 AC 288 ms
122,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;


struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x((x% mod + mod) % mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};


long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

mint COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return mint(fac[n] * (finv[k] * finv[n - k] % mod) % mod);
}

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll N, K; scanf("%lld %lld", &N, &K);
	mint sum = N * (N - 1) / 2 % mod;
	COMinit();
	mint res = 0;
	res += sum * COM(N - 1, K) * fac[K];
	if(K > 1) res += sum *COM(K, 2) * COM(N - 2, K - 2) * fac[K - 2];
	res += COM(N - 1, K) * fac[K] * N;
	cout << res.x << endl;
	return 0;
}
0