Luogu P3799 demon dream sticks and p3799 sticks

Source: Internet
Author: User

Luogu P3799 demon dream sticks and p3799 sticks

In the above question, the demon dream chopped a wooden stick, and now she wants to combine the wooden sticks.


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

For 30% of Data N <= 5000

For 100% of Data N <= 100000



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("","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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.