洛谷P4492 [HAOI2018]苹果树(组合数)

题意

题目链接

Sol

有点自闭,。我好像对组合数一窍不通(~~~~)

Orz shadowice

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
int mod;
template<typename A, typename B> inline bool chmax(A &x, B y) {return x < y ? x = y, 1 : 0;}
template<typename A, typename B> inline bool chmin(A &x, B y) {return x > y ? x = y, 1 : 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
#define LL long long
using namespace std;
const int MAXN = 2001, INF = 1e9 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
LL N, fac[MAXN], C[MAXN][MAXN];
signed main() {
N = read(); mod = read();
fac[0] = 1;
for(int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1]);
for(int i = 0; i <= N; i++) {
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
}
LL res = 0;
for(int i = 2; i <= N; i++) {
LL ans = 0;
for(int j = 1; j <= N - i + 1; j++) {
add2(ans, 1ll * fac[j] % mod * C[N - i][j - 1] % mod * j % mod * (N - j) % mod * fac[N - j - 1] % mod * i % mod * (i - 1) % mod);
}
add2(res, ans);
}
cout << res;
return 0;
}
/*
6
2 5 2 3 5 4
1
1
1
2
4
*/
上一篇:BZOJ.5305.[HAOI2018]苹果树(组合 计数)


下一篇:WinFrom ProgressBar控件的使用