洛谷P3799 小 Y 拼木棒(暴力、组合数学)

题目来源:https://www.luogu.com.cn/problem/P3799

`

define int long long

using namespace std;
int a[5010];
const int MOD = 1e9 + 7;//不要忘记取模

int Ccal(int x, int k) {
if (k == 1) { return x % MOD; }
if (k == 2) { return x * (x - 1) / 2 % MOD; }
}

signed main() {
int n; cin >> n; int maxn = 0;
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) {
int x; cin >> x;
a[x]++; maxn = max(maxn, x);
}
int ans = 0;
for (int i = 2; i <= maxn; i++) {
if (a[i] >= 2) {
for (int j = 1; j <= i / 2; j++) {
if (j == i - j) {
if (a[j] >= 2) { ans += (Ccal(a[i], 2) * Ccal(a[j], 2) % MOD); }
}
else {
if (a[j] >= 1 && a[i - j] >= 1) { ans += (Ccal(a[i], 2) * Ccal(a[j], 1) * Ccal(a[i - j], 1) % MOD); }//是否大于1一定要判,不然会有负数出现
}
ans %= MOD;
}//j<=i/2是为了防止1 3和3 1这种相同情况重复判的问题,不可以在后面搞个ans/2来解决,因为j==i-j的中位情况只判一次。
}
}
cout << ans << endl;
return 0;
}`

posted @ 2025-08-05 13:08  yubai111  阅读(12)  评论(0)    收藏  举报