Submission #20338

# Submission time Handle Problem Language Result Execution time Memory
20338 2017-02-07T04:50:56 Z admin 복불복 (OJUZ11_luck) C++14
100 / 100
172 ms 2344 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
#include <time.h>
#include <functional>
#include <numeric>

using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

#define debug(format, ...) printf(format, __VA_ARGS__);

const int N_ = 350;
const int MOD = (int)1e9 + 7;

int N, K;
int A[N_], B[N_];

int comb[N_][N_];
int fac[N_];

struct mint {
	ll val;
	mint() { val = 0; }
	mint(ll val): val(val % MOD) { val = 0; }
	~mint() { }
	ll& operator*()	{ return val;}
	mint operator+ (mint t) { return val + t.val; }
	mint operator- (mint t) { return val - t.val + MOD; }
	mint operator* (mint t) { return val * t.val; }
	mint operator+= (mint t) { return *this = (val + t.val); }
	mint operator-= (mint t) { return *this = (val - t.val); }
};

int bound[N_];

mint ta[N_][N_];
mint tb[N_][N_];

int ha[N_];

ll nat (ll x) {
	return (x < 0) ? 0 : x;
}

mint count_cases (int L, int R) {
	mint ret = 0;

	// bound 구하기
	for(int i = K, cur = 1; i > 0; i--) {
		while(cur <= N && B[cur] <= L - A[i]) cur += 1;
		if(cur == 1) return 0;
		bound[i] = cur - 1;
	}
	for(int i = K+1, cur = N; i <= N; i++) {
		while(cur > 0 && B[cur] >= R - A[i]) cur -= 1;
		if(cur == N) return 0;
		bound[i] = cur + 1;
	}


	// prep
	for(int j = 1; j <= N; j++) {
		ha[j] = 0;
		for(int i = 1; i <= K; i++) if(j <= bound[i]) ha[j] += 1;
	}

	for(int k = 0; k <= K; k++) {
		// ta
		fill(ta[k], ta[k] + N + 2, 0);
		if(k == 0) {
			for(int j = 1; j <= N+1; j++) ta[k][j] = 1;
		}else {
			for(int j = N+1; j >= bound[K+1]; j--) {
				ta[k][j] = ta[k][j+1];
				if(ha[j] >= k) ta[k][j] += ta[k-1][j+1] * nat(ha[j] - k + 1);
			}
		}

		tb[K][K-k] = ta[k][bound[K+1]] * fac[K-k];
	}

	for(int i = K+1; i <= N; i++) {
		int d = (i == K+1) ? 0 : (bound[i-1] - bound[i]);
		for(int c = 0; c <= K; c++) {
			tb[i][c] = 0;
			for(int z = 0; z <= d && c+z <= K; z++)
				tb[i][c] += tb[i-1][c+z] * comb[d][z] * nat((N - bound[i] + 1) - (i-1-c));
		}
	}

	for(int z = 0; z <= bound[N]-1 && z <= K; z++)
		ret += tb[N][z] * comb[bound[N]-1][z];

	return ret;
}

/* 서브태스크 */
// 1. n <= 8 (n!) -O
// 2. n <= 100, k = 1 (n) -X
// 3. n <= 15 (n^3 2^n)
// 4. k <= 8 (n^3 k^2)
// 5. n <= 100 (n^3 k) -O

int main() {
	scanf("%d%d", &N, &K);
	for(int i = 1; i <= N; i++) scanf("%d", A+i);
	for(int i = 1; i <= N; i++) scanf("%d", B+i);
	sort(A+1, A+N+1, greater<int>());
	sort(B+1, B+N+1, greater<int>());
	for(int i = N; i > 0; i--) A[i] = A[1] - A[i], B[i] = B[1] - B[i];

	// nCr
	comb[0][0] = 1;
	for(int i = 1; i <= N; i++) {
		comb[i][0] = comb[i][i] = 1;
		for(int j = 1; j < i; j++) comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) % MOD;	
	}


	fac[0] = 1;
	for(int i = 1; i <= N; i++)
		fac[i] = ((ll)fac[i-1] * i) % MOD;

	if(N == K)
		return 0 & printf("%d\n", fac[N]);

	mint ans = 0;
	for(int X = A[K] + B[1]; X <= A[N] + B[N]; X++) {
		ans += count_cases(X, X) - count_cases(X-1, X);
	}

	printf("%lld\n", *ans);

	return 0;
}

Compilation message

luck.cpp: In function 'int main()':
luck.cpp:127:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
                       ^
luck.cpp:128:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= N; i++) scanf("%d", A+i);
                                              ^
luck.cpp:129:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i <= N; i++) scanf("%d", B+i);
                                              ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2188 KB Output is correct
3 Correct 5 ms 2188 KB Output is correct
4 Correct 4 ms 2188 KB Output is correct
5 Correct 4 ms 2192 KB Output is correct
6 Correct 3 ms 2192 KB Output is correct
7 Correct 4 ms 2192 KB Output is correct
8 Correct 3 ms 2192 KB Output is correct
9 Correct 4 ms 2192 KB Output is correct
10 Correct 4 ms 2320 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 3 ms 2320 KB Output is correct
13 Correct 2 ms 2320 KB Output is correct
14 Correct 3 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 4 ms 2320 KB Output is correct
17 Correct 3 ms 2320 KB Output is correct
18 Correct 4 ms 2320 KB Output is correct
19 Correct 4 ms 2320 KB Output is correct
20 Correct 4 ms 2320 KB Output is correct
21 Correct 3 ms 2320 KB Output is correct
22 Correct 4 ms 2320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 2340 KB Output is correct
2 Correct 4 ms 2340 KB Output is correct
3 Correct 4 ms 2340 KB Output is correct
4 Correct 5 ms 2340 KB Output is correct
5 Correct 6 ms 2340 KB Output is correct
6 Correct 5 ms 2344 KB Output is correct
7 Correct 7 ms 2344 KB Output is correct
8 Correct 4 ms 2344 KB Output is correct
9 Correct 5 ms 2344 KB Output is correct
10 Correct 3 ms 2344 KB Output is correct
11 Correct 3 ms 2344 KB Output is correct
12 Correct 5 ms 2344 KB Output is correct
13 Correct 3 ms 2344 KB Output is correct
14 Correct 11 ms 2344 KB Output is correct
15 Correct 6 ms 2344 KB Output is correct
16 Correct 12 ms 2344 KB Output is correct
17 Correct 14 ms 2344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2188 KB Output is correct
3 Correct 5 ms 2188 KB Output is correct
4 Correct 4 ms 2188 KB Output is correct
5 Correct 4 ms 2192 KB Output is correct
6 Correct 3 ms 2192 KB Output is correct
7 Correct 4 ms 2192 KB Output is correct
8 Correct 3 ms 2192 KB Output is correct
9 Correct 4 ms 2192 KB Output is correct
10 Correct 4 ms 2320 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 3 ms 2320 KB Output is correct
13 Correct 2 ms 2320 KB Output is correct
14 Correct 3 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 4 ms 2320 KB Output is correct
17 Correct 3 ms 2320 KB Output is correct
18 Correct 4 ms 2320 KB Output is correct
19 Correct 4 ms 2320 KB Output is correct
20 Correct 4 ms 2320 KB Output is correct
21 Correct 3 ms 2320 KB Output is correct
22 Correct 4 ms 2320 KB Output is correct
23 Correct 5 ms 2344 KB Output is correct
24 Correct 3 ms 2344 KB Output is correct
25 Correct 7 ms 2344 KB Output is correct
26 Correct 6 ms 2344 KB Output is correct
27 Correct 3 ms 2344 KB Output is correct
28 Correct 4 ms 2344 KB Output is correct
29 Correct 7 ms 2344 KB Output is correct
30 Correct 4 ms 2344 KB Output is correct
31 Correct 6 ms 2344 KB Output is correct
32 Correct 5 ms 2344 KB Output is correct
33 Correct 4 ms 2344 KB Output is correct
34 Correct 3 ms 2344 KB Output is correct
35 Correct 5 ms 2344 KB Output is correct
36 Correct 5 ms 2344 KB Output is correct
37 Correct 6 ms 2344 KB Output is correct
38 Correct 6 ms 2344 KB Output is correct
39 Correct 4 ms 2344 KB Output is correct
40 Correct 5 ms 2344 KB Output is correct
41 Correct 5 ms 2344 KB Output is correct
42 Correct 6 ms 2344 KB Output is correct
43 Correct 3 ms 2344 KB Output is correct
44 Correct 3 ms 2344 KB Output is correct
45 Correct 4 ms 2344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2188 KB Output is correct
3 Correct 5 ms 2188 KB Output is correct
4 Correct 4 ms 2188 KB Output is correct
5 Correct 4 ms 2192 KB Output is correct
6 Correct 3 ms 2192 KB Output is correct
7 Correct 4 ms 2192 KB Output is correct
8 Correct 3 ms 2192 KB Output is correct
9 Correct 4 ms 2192 KB Output is correct
10 Correct 4 ms 2320 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 3 ms 2320 KB Output is correct
13 Correct 2 ms 2320 KB Output is correct
14 Correct 3 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 4 ms 2320 KB Output is correct
17 Correct 3 ms 2320 KB Output is correct
18 Correct 4 ms 2320 KB Output is correct
19 Correct 4 ms 2320 KB Output is correct
20 Correct 4 ms 2320 KB Output is correct
21 Correct 3 ms 2320 KB Output is correct
22 Correct 4 ms 2320 KB Output is correct
23 Correct 11 ms 2340 KB Output is correct
24 Correct 4 ms 2340 KB Output is correct
25 Correct 4 ms 2340 KB Output is correct
26 Correct 5 ms 2340 KB Output is correct
27 Correct 6 ms 2340 KB Output is correct
28 Correct 5 ms 2344 KB Output is correct
29 Correct 7 ms 2344 KB Output is correct
30 Correct 4 ms 2344 KB Output is correct
31 Correct 5 ms 2344 KB Output is correct
32 Correct 3 ms 2344 KB Output is correct
33 Correct 3 ms 2344 KB Output is correct
34 Correct 5 ms 2344 KB Output is correct
35 Correct 3 ms 2344 KB Output is correct
36 Correct 11 ms 2344 KB Output is correct
37 Correct 6 ms 2344 KB Output is correct
38 Correct 12 ms 2344 KB Output is correct
39 Correct 14 ms 2344 KB Output is correct
40 Correct 5 ms 2344 KB Output is correct
41 Correct 3 ms 2344 KB Output is correct
42 Correct 7 ms 2344 KB Output is correct
43 Correct 6 ms 2344 KB Output is correct
44 Correct 3 ms 2344 KB Output is correct
45 Correct 4 ms 2344 KB Output is correct
46 Correct 7 ms 2344 KB Output is correct
47 Correct 4 ms 2344 KB Output is correct
48 Correct 6 ms 2344 KB Output is correct
49 Correct 5 ms 2344 KB Output is correct
50 Correct 4 ms 2344 KB Output is correct
51 Correct 3 ms 2344 KB Output is correct
52 Correct 5 ms 2344 KB Output is correct
53 Correct 5 ms 2344 KB Output is correct
54 Correct 6 ms 2344 KB Output is correct
55 Correct 6 ms 2344 KB Output is correct
56 Correct 4 ms 2344 KB Output is correct
57 Correct 5 ms 2344 KB Output is correct
58 Correct 5 ms 2344 KB Output is correct
59 Correct 6 ms 2344 KB Output is correct
60 Correct 3 ms 2344 KB Output is correct
61 Correct 3 ms 2344 KB Output is correct
62 Correct 4 ms 2344 KB Output is correct
63 Correct 43 ms 2344 KB Output is correct
64 Correct 7 ms 2344 KB Output is correct
65 Correct 7 ms 2344 KB Output is correct
66 Correct 37 ms 2344 KB Output is correct
67 Correct 40 ms 2344 KB Output is correct
68 Correct 39 ms 2344 KB Output is correct
69 Correct 21 ms 2344 KB Output is correct
70 Correct 29 ms 2344 KB Output is correct
71 Correct 43 ms 2344 KB Output is correct
72 Correct 35 ms 2344 KB Output is correct
73 Correct 20 ms 2344 KB Output is correct
74 Correct 17 ms 2344 KB Output is correct
75 Correct 16 ms 2344 KB Output is correct
76 Correct 18 ms 2344 KB Output is correct
77 Correct 34 ms 2344 KB Output is correct
78 Correct 4 ms 2344 KB Output is correct
79 Correct 4 ms 2344 KB Output is correct
80 Correct 8 ms 2344 KB Output is correct
81 Correct 14 ms 2344 KB Output is correct
82 Correct 29 ms 2344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2188 KB Output is correct
3 Correct 5 ms 2188 KB Output is correct
4 Correct 4 ms 2188 KB Output is correct
5 Correct 4 ms 2192 KB Output is correct
6 Correct 3 ms 2192 KB Output is correct
7 Correct 4 ms 2192 KB Output is correct
8 Correct 3 ms 2192 KB Output is correct
9 Correct 4 ms 2192 KB Output is correct
10 Correct 4 ms 2320 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 3 ms 2320 KB Output is correct
13 Correct 2 ms 2320 KB Output is correct
14 Correct 3 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 4 ms 2320 KB Output is correct
17 Correct 3 ms 2320 KB Output is correct
18 Correct 4 ms 2320 KB Output is correct
19 Correct 4 ms 2320 KB Output is correct
20 Correct 4 ms 2320 KB Output is correct
21 Correct 3 ms 2320 KB Output is correct
22 Correct 4 ms 2320 KB Output is correct
23 Correct 11 ms 2340 KB Output is correct
24 Correct 4 ms 2340 KB Output is correct
25 Correct 4 ms 2340 KB Output is correct
26 Correct 5 ms 2340 KB Output is correct
27 Correct 6 ms 2340 KB Output is correct
28 Correct 5 ms 2344 KB Output is correct
29 Correct 7 ms 2344 KB Output is correct
30 Correct 4 ms 2344 KB Output is correct
31 Correct 5 ms 2344 KB Output is correct
32 Correct 3 ms 2344 KB Output is correct
33 Correct 3 ms 2344 KB Output is correct
34 Correct 5 ms 2344 KB Output is correct
35 Correct 3 ms 2344 KB Output is correct
36 Correct 11 ms 2344 KB Output is correct
37 Correct 6 ms 2344 KB Output is correct
38 Correct 12 ms 2344 KB Output is correct
39 Correct 14 ms 2344 KB Output is correct
40 Correct 5 ms 2344 KB Output is correct
41 Correct 3 ms 2344 KB Output is correct
42 Correct 7 ms 2344 KB Output is correct
43 Correct 6 ms 2344 KB Output is correct
44 Correct 3 ms 2344 KB Output is correct
45 Correct 4 ms 2344 KB Output is correct
46 Correct 7 ms 2344 KB Output is correct
47 Correct 4 ms 2344 KB Output is correct
48 Correct 6 ms 2344 KB Output is correct
49 Correct 5 ms 2344 KB Output is correct
50 Correct 4 ms 2344 KB Output is correct
51 Correct 3 ms 2344 KB Output is correct
52 Correct 5 ms 2344 KB Output is correct
53 Correct 5 ms 2344 KB Output is correct
54 Correct 6 ms 2344 KB Output is correct
55 Correct 6 ms 2344 KB Output is correct
56 Correct 4 ms 2344 KB Output is correct
57 Correct 5 ms 2344 KB Output is correct
58 Correct 5 ms 2344 KB Output is correct
59 Correct 6 ms 2344 KB Output is correct
60 Correct 3 ms 2344 KB Output is correct
61 Correct 3 ms 2344 KB Output is correct
62 Correct 4 ms 2344 KB Output is correct
63 Correct 43 ms 2344 KB Output is correct
64 Correct 7 ms 2344 KB Output is correct
65 Correct 7 ms 2344 KB Output is correct
66 Correct 37 ms 2344 KB Output is correct
67 Correct 40 ms 2344 KB Output is correct
68 Correct 39 ms 2344 KB Output is correct
69 Correct 21 ms 2344 KB Output is correct
70 Correct 29 ms 2344 KB Output is correct
71 Correct 43 ms 2344 KB Output is correct
72 Correct 35 ms 2344 KB Output is correct
73 Correct 20 ms 2344 KB Output is correct
74 Correct 17 ms 2344 KB Output is correct
75 Correct 16 ms 2344 KB Output is correct
76 Correct 18 ms 2344 KB Output is correct
77 Correct 34 ms 2344 KB Output is correct
78 Correct 4 ms 2344 KB Output is correct
79 Correct 4 ms 2344 KB Output is correct
80 Correct 8 ms 2344 KB Output is correct
81 Correct 14 ms 2344 KB Output is correct
82 Correct 29 ms 2344 KB Output is correct
83 Correct 3 ms 2344 KB Output is correct
84 Correct 82 ms 2344 KB Output is correct
85 Correct 118 ms 2344 KB Output is correct
86 Correct 47 ms 2344 KB Output is correct
87 Correct 37 ms 2344 KB Output is correct
88 Correct 135 ms 2344 KB Output is correct
89 Correct 123 ms 2344 KB Output is correct
90 Correct 35 ms 2344 KB Output is correct
91 Correct 24 ms 2344 KB Output is correct
92 Correct 11 ms 2344 KB Output is correct
93 Correct 164 ms 2344 KB Output is correct
94 Correct 19 ms 2344 KB Output is correct
95 Correct 172 ms 2344 KB Output is correct
96 Correct 169 ms 2344 KB Output is correct
97 Correct 143 ms 2344 KB Output is correct
98 Correct 27 ms 2344 KB Output is correct
99 Correct 3 ms 2344 KB Output is correct
100 Correct 24 ms 2344 KB Output is correct
101 Correct 134 ms 2344 KB Output is correct
102 Correct 162 ms 2344 KB Output is correct
103 Correct 163 ms 2344 KB Output is correct
104 Correct 29 ms 2344 KB Output is correct
105 Correct 126 ms 2344 KB Output is correct
106 Correct 66 ms 2344 KB Output is correct
107 Correct 141 ms 2344 KB Output is correct