답안 #19950

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
19950 2016-02-25T07:45:29 Z tonyjjw 순열 (kriii4_T) C++14
100 / 100
479 ms 40784 KB
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <limits.h>
#include <locale.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>
#include <unordered_set>
#include <unordered_map>

#pragma warning(disable:4996)
using namespace std;

#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;

int IT_MAX = 131072;
const ll MOD = 1000000007;
const int INF = 1034567891;
const ll LL_INF = 2234567890123456789ll;
const db PI = 3.141592653589793238;
const ldb ERR = 1E-10;

ll mul_inv(ll a, ll b)
{
	ll b0 = b, t, q;
	ll x0 = 0, x1 = 1;
	if (b == 1) return 1;
	while (a > 1) {
		q = a / b;
		t = b, b = a % b, a = t;
		t = x0, x0 = x1 - q * x0, x1 = t;
	}
	if (x1 < 0) x1 += b0;
	return x1;
}

ll F[1000050];
ll Finv[1000050];
ll dp1[1000050];
ll dp2[1000050];
ll dp3[1000050];

inline ll Comb(int x, int y) {
	ll rv = F[x];
	rv = (rv * Finv[y]) % MOD;
	rv = (rv * Finv[x - y]) % MOD;
	return rv;
}

int main() {
	int N, L, K, i;
	F[0] = 1;
	for (i = 1; i <= 1000010; i++) F[i] = (F[i - 1] * i) % MOD;
	for (i = 0; i <= 1000010; i++) Finv[i] = mul_inv(F[i], MOD);

	scanf("%d %d", &N, &K);
	K++;
	if (K == N + 1) return !printf("0\n");


	ll ans = Comb(N - K + 2, 2) * N % MOD;
	ans -= 2 * Comb(N - K + 2, 3);
	ans = (ans + 2 * MOD) % MOD;
	ans = (ans*N) % MOD;
	ans = (ans*F[N - 1]) % MOD;
	for (L = K; L < N; L++) {
		dp1[L] = Comb(N + 1, L + 1) * L % MOD;
		
		dp2[L] = Comb(N + 2, L + 2);
		dp2[L] = dp2[L] * L % MOD * (L + 1) % MOD;
		dp2[L] = (dp2[L] - dp1[L] + MOD) % MOD;

		dp3[L] = Comb(N + 3, L + 3) * L % MOD;
		dp3[L] = dp3[L] * (L + 1) % MOD * (L + 2) % MOD;
		dp3[L] = (dp3[L] - 3 * dp2[L] + 3 * MOD) % MOD;
		dp3[L] = (dp3[L] - 2 * dp1[L] + 2 * MOD) % MOD;

		ll t = Comb(L - K + 2, 2)*L%MOD;
		t -= 2 * Comb(L - K + 2, 3);
		t = (t + 2 * MOD) % MOD;
		t = (t * F[N - L - 1]) % MOD;
		t = (t * F[L-1]) % MOD;
		
		ll t2 = 0;
		t2 += (2 * N * dp1[L]) % MOD;
		t2 -= 2 * dp2[L];
		t2 += (ll)N*(N - 1) % MOD*dp1[L] % MOD;
		t2 -= (2 * N - 1) * dp2[L] % MOD;
		t2 += dp3[L];
		t2 = (t2 + 100 * MOD) % MOD;

		ans += (t*t2) % MOD;
	}

	ans = ans % MOD;

	ll sum = 0;
	for (i = 1; i + K - 1 <= N; i++) sum += (N - i - K + 2);
	sum %= MOD;
	sum *= N + 1;
	sum %= MOD;
	sum = (sum * F[N]) % MOD;

	ans = sum - ans;
	ans %= MOD;
	ans += MOD;
	ans %= MOD;

	printf("%lld\n", ans);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 40784 KB Output is correct
2 Correct 374 ms 40784 KB Output is correct
3 Correct 374 ms 40784 KB Output is correct
4 Correct 378 ms 40784 KB Output is correct
5 Correct 379 ms 40784 KB Output is correct
6 Correct 371 ms 40784 KB Output is correct
7 Correct 377 ms 40784 KB Output is correct
8 Correct 375 ms 40784 KB Output is correct
9 Correct 379 ms 40784 KB Output is correct
10 Correct 379 ms 40784 KB Output is correct
11 Correct 375 ms 40784 KB Output is correct
12 Correct 379 ms 40784 KB Output is correct
13 Correct 371 ms 40784 KB Output is correct
14 Correct 378 ms 40784 KB Output is correct
15 Correct 375 ms 40784 KB Output is correct
16 Correct 371 ms 40784 KB Output is correct
17 Correct 378 ms 40784 KB Output is correct
18 Correct 375 ms 40784 KB Output is correct
19 Correct 374 ms 40784 KB Output is correct
20 Correct 375 ms 40784 KB Output is correct
21 Correct 378 ms 40784 KB Output is correct
22 Correct 378 ms 40784 KB Output is correct
23 Correct 378 ms 40784 KB Output is correct
24 Correct 374 ms 40784 KB Output is correct
25 Correct 375 ms 40784 KB Output is correct
26 Correct 378 ms 40784 KB Output is correct
27 Correct 379 ms 40784 KB Output is correct
28 Correct 375 ms 40784 KB Output is correct
29 Correct 370 ms 40784 KB Output is correct
30 Correct 379 ms 40784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 460 ms 40784 KB Output is correct
2 Correct 405 ms 40784 KB Output is correct
3 Correct 386 ms 40784 KB Output is correct
4 Correct 380 ms 40784 KB Output is correct
5 Correct 388 ms 40784 KB Output is correct
6 Correct 382 ms 40784 KB Output is correct
7 Correct 441 ms 40784 KB Output is correct
8 Correct 420 ms 40784 KB Output is correct
9 Correct 394 ms 40784 KB Output is correct
10 Correct 368 ms 40784 KB Output is correct
11 Correct 407 ms 40784 KB Output is correct
12 Correct 374 ms 40784 KB Output is correct
13 Correct 389 ms 40784 KB Output is correct
14 Correct 440 ms 40784 KB Output is correct
15 Correct 412 ms 40784 KB Output is correct
16 Correct 430 ms 40784 KB Output is correct
17 Correct 399 ms 40784 KB Output is correct
18 Correct 402 ms 40784 KB Output is correct
19 Correct 384 ms 40784 KB Output is correct
20 Correct 413 ms 40784 KB Output is correct
21 Correct 398 ms 40784 KB Output is correct
22 Correct 431 ms 40784 KB Output is correct
23 Correct 400 ms 40784 KB Output is correct
24 Correct 386 ms 40784 KB Output is correct
25 Correct 386 ms 40784 KB Output is correct
26 Correct 410 ms 40784 KB Output is correct
27 Correct 378 ms 40784 KB Output is correct
28 Correct 379 ms 40784 KB Output is correct
29 Correct 476 ms 40784 KB Output is correct
30 Correct 479 ms 40784 KB Output is correct