Luogu P3799 demon dream sticks and p3799 sticks
Background
In the above question, the demon dream chopped a wooden stick, and now she wants to combine the wooden sticks.
Description
There are n wooden sticks. Now, select 4 from them and want to form a triangle. How can I select them?
Input/Output Format
Input Format:
The first line is an integer n.
N integers in the second line, a1, a2 ,...... An (0 <ai <= 5000) represents the length of each wooden rod.
Output Format:
One integer in a row, modulo the 1e9 + 7
Input and Output sample input sample #1: Copy
4 1 1 2 2
Output example #1: Copy
1
Description
For 30% of Data N <= 5000
For 100% of Data N <= 100000
By-szc
Sure enough, mathematics is still a short board (in fact, I am very spam)
This question should be a bare combination of numbers.
Enumerate short edges and query long edges
Combination quantity pre-processing
// luogu-judger-enable-o2#include<iostream>#include<cstdio>#define LL long long using namespace std;const LL MAXN=2*1e6+10;const LL mod=1e9+7;LL N;LL a[MAXN],h[MAXN];LL C1(LL a){return a;}LL C2(LL a){return ( a*(a-1) )/2; }int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif LL ans=0; scanf("%lld",&N); for(LL i=1;i<=N;i++) scanf("%lld",&a[i]),h[ a[i] ] ++; for(LL i=1;i<=5000;i++) for(LL j=i;j<=5000&&i+j<=5000;j++) { if(i==j) { if(h[i]>=2&&h[i*2]>=2) ans+=C2(h[i])*C2(h[i*2])%mod; } else { if(h[i]>=1&&h[j]>=1&&h[i+j]>=2) ans+=C1(h[i])*C1(h[j])*C2(h[i+j])%mod; } } printf("%lld",ans%mod); return 0;}