답안 #509630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
509630 2022-01-14T07:39:20 Z hollwo_pelw NoM (RMI21_nom) C++17
100 / 100
139 ms 31820 KB
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}

void Hollwo_Pelw();

signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}

template<class T> struct mod_int {
	static constexpr int mod() { return T::mod; }

	int v;
	mod_int(long long _v = 0) 
		: v(norm(_v)) {}

	inline int norm(long long a) {
		return a < 0 ? a % mod() + mod() : a % mod();
	}
	mod_int neg() const { return v == 0 ? 0 : mod() - v; }

	template <typename U>
	explicit operator U() const { return v; }

	mod_int operator - () const { return neg(); }
	mod_int operator + () const { return mod_int(*this); }

	mod_int& operator -- () { if (v == 0) v = mod(); -- v; return *this; }
	friend mod_int operator -- (mod_int& a, int) { mod_int r = a; --a; return r; }
	mod_int& operator ++ () { ++ v; if (v == mod()) v = 0; return *this; }
	friend mod_int operator ++ (mod_int& a, int) { mod_int r = a; ++a; return r; }
	
#define defop(type, op) \
	friend inline type operator op (type a, const type &b) { return a op##= b; } \
	type& operator op##= (const type &oth)

	defop(mod_int, +) {
		if ((v += oth.v) >= mod()) v -= mod();
		return *this;
	}
	defop(mod_int, -) {
		if ((v -= oth.v) < 0) v += mod();
		return *this;
	}
	defop(mod_int, *) {
		return *this = norm(1ll * v * oth.v);
	}

	mod_int pow(long long b) const {
		mod_int a = *this, r = 1;
		while (b) {
			if (b&1) r *= a;
			b >>= 1; a *= a;
		}
		return r;
	}
	mod_int inv() const { return pow(mod() - 2); }

	defop(mod_int, /) { 
		return *this *= oth.inv();
	}
#undef defop

#define compare(op) friend bool operator op \
(const mod_int& lhs, const mod_int& rhs) { return lhs.v op rhs.v; }
	compare(==) compare(!=) compare(<) compare(<=) compare(>) compare(>=)
#undef compare

	friend std::ostream& operator << (std::ostream& out, const mod_int& n) { return out << int(n); }
	friend std::istream& operator >> (std::istream& inp, mod_int& n) { long long x; inp >> x; n = mod_int(x); return inp; }
};

struct modular { static constexpr int mod = static_cast<int>(1e9 + 7); };

// struct modular { static int mod; };
// int modular::mod;

using Mint = mod_int<modular>;

const int N = 2005;

int n, m, cnt[N];
Mint dp[N][N * 2], fac[N * 2], inv[N * 2];

inline Mint C(int n, int k) {
	return n < 0 || k > n ? 0 : fac[n] * inv[n - k] * inv[k];
}

void Hollwo_Pelw() {
	fac[0] = 1;
	for (int i = 1; i < N * 2; i++)
		fac[i] = fac[i - 1] * i;
	inv[N * 2 - 1] = fac[N * 2 - 1].inv();
	for (int i = N * 2 - 1; i; i--)
		inv[i - 1] = inv[i] * i;

	cin >> n >> m;
	for (int i = 0; i < 2 * n; i++)
		cnt[i % m] ++;

	dp[0][0] = 1;
	for (int i = 0, sum = 0; i < m; i++) {
		for (int j = 0; j <= sum; j++) if (dp[i][j])
			for (int p = 0; p * 2 <= cnt[i]; p ++)
				dp[i + 1][j + p] += dp[i][j] * C(n - j, p) * fac[cnt[i]] * inv[cnt[i] - p * 2]; 
		sum += cnt[i];
	}
	for (int i = n; ~i; i--) {
		dp[m][i] *= fac[2 * n - 2 * i];
		for (int j = i + 1; j <= n; j++)
			dp[m][i] -= C(j, i) * dp[m][j];
	}
	cout << dp[m][0];
}

Compilation message

Main.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
Main.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31820 KB Output is correct
2 Correct 13 ms 31764 KB Output is correct
3 Correct 18 ms 31820 KB Output is correct
4 Correct 14 ms 31796 KB Output is correct
5 Correct 13 ms 31696 KB Output is correct
6 Correct 13 ms 31696 KB Output is correct
7 Correct 14 ms 31820 KB Output is correct
8 Correct 14 ms 31692 KB Output is correct
9 Correct 15 ms 31700 KB Output is correct
10 Correct 16 ms 31724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31820 KB Output is correct
2 Correct 13 ms 31764 KB Output is correct
3 Correct 18 ms 31820 KB Output is correct
4 Correct 14 ms 31796 KB Output is correct
5 Correct 13 ms 31696 KB Output is correct
6 Correct 13 ms 31696 KB Output is correct
7 Correct 14 ms 31820 KB Output is correct
8 Correct 14 ms 31692 KB Output is correct
9 Correct 15 ms 31700 KB Output is correct
10 Correct 16 ms 31724 KB Output is correct
11 Correct 12 ms 31696 KB Output is correct
12 Correct 12 ms 31820 KB Output is correct
13 Correct 12 ms 31708 KB Output is correct
14 Correct 12 ms 31692 KB Output is correct
15 Correct 12 ms 31796 KB Output is correct
16 Correct 14 ms 31804 KB Output is correct
17 Correct 13 ms 31820 KB Output is correct
18 Correct 16 ms 31692 KB Output is correct
19 Correct 13 ms 31764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31820 KB Output is correct
2 Correct 13 ms 31764 KB Output is correct
3 Correct 18 ms 31820 KB Output is correct
4 Correct 14 ms 31796 KB Output is correct
5 Correct 13 ms 31696 KB Output is correct
6 Correct 13 ms 31696 KB Output is correct
7 Correct 14 ms 31820 KB Output is correct
8 Correct 14 ms 31692 KB Output is correct
9 Correct 15 ms 31700 KB Output is correct
10 Correct 16 ms 31724 KB Output is correct
11 Correct 12 ms 31696 KB Output is correct
12 Correct 12 ms 31820 KB Output is correct
13 Correct 12 ms 31708 KB Output is correct
14 Correct 12 ms 31692 KB Output is correct
15 Correct 12 ms 31796 KB Output is correct
16 Correct 14 ms 31804 KB Output is correct
17 Correct 13 ms 31820 KB Output is correct
18 Correct 16 ms 31692 KB Output is correct
19 Correct 13 ms 31764 KB Output is correct
20 Correct 16 ms 31820 KB Output is correct
21 Correct 17 ms 31744 KB Output is correct
22 Correct 17 ms 31800 KB Output is correct
23 Correct 16 ms 31752 KB Output is correct
24 Correct 18 ms 31692 KB Output is correct
25 Correct 15 ms 31820 KB Output is correct
26 Correct 15 ms 31816 KB Output is correct
27 Correct 16 ms 31820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31820 KB Output is correct
2 Correct 13 ms 31764 KB Output is correct
3 Correct 18 ms 31820 KB Output is correct
4 Correct 14 ms 31796 KB Output is correct
5 Correct 13 ms 31696 KB Output is correct
6 Correct 13 ms 31696 KB Output is correct
7 Correct 14 ms 31820 KB Output is correct
8 Correct 14 ms 31692 KB Output is correct
9 Correct 15 ms 31700 KB Output is correct
10 Correct 16 ms 31724 KB Output is correct
11 Correct 12 ms 31696 KB Output is correct
12 Correct 12 ms 31820 KB Output is correct
13 Correct 12 ms 31708 KB Output is correct
14 Correct 12 ms 31692 KB Output is correct
15 Correct 12 ms 31796 KB Output is correct
16 Correct 14 ms 31804 KB Output is correct
17 Correct 13 ms 31820 KB Output is correct
18 Correct 16 ms 31692 KB Output is correct
19 Correct 13 ms 31764 KB Output is correct
20 Correct 16 ms 31820 KB Output is correct
21 Correct 17 ms 31744 KB Output is correct
22 Correct 17 ms 31800 KB Output is correct
23 Correct 16 ms 31752 KB Output is correct
24 Correct 18 ms 31692 KB Output is correct
25 Correct 15 ms 31820 KB Output is correct
26 Correct 15 ms 31816 KB Output is correct
27 Correct 16 ms 31820 KB Output is correct
28 Correct 20 ms 31804 KB Output is correct
29 Correct 24 ms 31820 KB Output is correct
30 Correct 27 ms 31800 KB Output is correct
31 Correct 22 ms 31692 KB Output is correct
32 Correct 25 ms 31796 KB Output is correct
33 Correct 27 ms 31820 KB Output is correct
34 Correct 42 ms 31776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 31820 KB Output is correct
2 Correct 13 ms 31764 KB Output is correct
3 Correct 18 ms 31820 KB Output is correct
4 Correct 14 ms 31796 KB Output is correct
5 Correct 13 ms 31696 KB Output is correct
6 Correct 13 ms 31696 KB Output is correct
7 Correct 14 ms 31820 KB Output is correct
8 Correct 14 ms 31692 KB Output is correct
9 Correct 15 ms 31700 KB Output is correct
10 Correct 16 ms 31724 KB Output is correct
11 Correct 12 ms 31696 KB Output is correct
12 Correct 12 ms 31820 KB Output is correct
13 Correct 12 ms 31708 KB Output is correct
14 Correct 12 ms 31692 KB Output is correct
15 Correct 12 ms 31796 KB Output is correct
16 Correct 14 ms 31804 KB Output is correct
17 Correct 13 ms 31820 KB Output is correct
18 Correct 16 ms 31692 KB Output is correct
19 Correct 13 ms 31764 KB Output is correct
20 Correct 16 ms 31820 KB Output is correct
21 Correct 17 ms 31744 KB Output is correct
22 Correct 17 ms 31800 KB Output is correct
23 Correct 16 ms 31752 KB Output is correct
24 Correct 18 ms 31692 KB Output is correct
25 Correct 15 ms 31820 KB Output is correct
26 Correct 15 ms 31816 KB Output is correct
27 Correct 16 ms 31820 KB Output is correct
28 Correct 20 ms 31804 KB Output is correct
29 Correct 24 ms 31820 KB Output is correct
30 Correct 27 ms 31800 KB Output is correct
31 Correct 22 ms 31692 KB Output is correct
32 Correct 25 ms 31796 KB Output is correct
33 Correct 27 ms 31820 KB Output is correct
34 Correct 42 ms 31776 KB Output is correct
35 Correct 58 ms 31800 KB Output is correct
36 Correct 112 ms 31800 KB Output is correct
37 Correct 59 ms 31800 KB Output is correct
38 Correct 82 ms 31800 KB Output is correct
39 Correct 86 ms 31692 KB Output is correct
40 Correct 139 ms 31800 KB Output is correct