Solution to a problem P3799 [demon] dream fight stick

Wwwwww unscrupulous propaganda about blog
Article list - nuclear fusion reactor core - Luo Gubo off


Knowledge Point: Combinatorial Mathematics / violence enumeration

  • Casual working:

    There \ (n \) wooden stick,
    Yoomu now from selected \ (4 \) root,
    wants to form an equilateral triangle ,
    asked several election law?

  • Analysis of the meaning of problems:

    Known by the casual working,
    to be a \ (4 \) wooden stick, composed of a regular triangle,
    then there must be two of the same length,
    and the length and the other two,
    equal to the length equal to two sticks before

    Also found that the length of each stick \ (a [i] \ leqslant
    5000 \) can be directly used two cycles,
    enumerate two kinds of stick length violence


  • Emotional understanding:

    The outer loop:
    • Many of the length from the first to \ (I \) of the two wooden removed,
      then the number of solutions:
      from \ (num [j] \) taken in the number of \ (2 \) a combination of number (i.e., \ ( C (NUM [J], 2) \) )
    The inner loop:
    • Is then removed from the two remaining stick in the
      sum of the lengths of \ (I \) sticks

      • If \ (J ij of == \) ,
        then from the length \ (J \) sticks removed \ (2 \) two
        then the number of solutions as follows:
        from \ (num [j] \) Remove the number of \ (2 \) a combination number
        , ie \ (C (num [j] , 2) \)

      • If \ (j \ ne ij \)
        then from the length \ (J \) sticks removed \ (1 \) a
        longer length from \ (ij of \) sticks removed \ (1 \) one
        Similarly, the number of programs:
        \ (C (NUM [J],. 1) \ C Times (NUM [ij of],. 1) \)

    According to the principle of multiplication,
    all multiplied by the number of programs,
    i.e., to obtain \ (ANS \) is the amount of change.

  • Analysis of Algorithms:

    Buckets \ ([I] NUM \) ,
    records of the length \ (I \) number of sticks occurring

    The outer loop:
    • Enumeration stick two equal length \ (I \) ,
      when the number of sticks of this length \ (\ geqslant 2 \) when
      entering the inner loop
    The inner loop:
    • Enum is used to synthesize a length of stick \ (J \)
      another wooden stick length is the \ (ij of \) .
      Two cases are discussed:

      • 1.如果 \((i==j \ ; \ num[j]\geqslant 1\ ;\ num[i-j]\geqslant 1)\) ,
        \(ans+=C(num[i] , 2) \times C(num[j] , 1)\times C(num[i-j] , 1)\)

      • 2. If \ ((ij of J == \; \ NUM [J] \ geqslant 2) \)
        \ (ANS = C + (NUM [I], 2) \ C Times (NUM [J],. 1) \)


Annex \ (AC \) Code:

#include<cstdio>
#include<ctype.h>
#define int long long
#define max(a,b) a>b?a:b
//======================================================= 
const int MARX = 1e6+10;
const int mod = 1e9+7;
int n,ans,maxa;
int a[MARX],num[MARX];
//=======================================================
inline int read()
{
    int fl=1,w=0;char ch=getchar();
    while(!isdigit(ch) && ch!='-') ch=getchar();
    if(ch=='-') fl=-1;
    while(isdigit(ch)){w=w*10+ch-'0',ch=getchar();}
    return fl*w;
}
inline int C(int x,int k)//求得从n个数中取出k个数的组合 
{
    return k==1?x:x*(x-1)/2;
}
//=======================================================
signed main()
{
    n=read();
    for(int i=1;i<=n;i++)//读入,并放入桶中 
      a[i]=read(),
      maxa=max(a[i],maxa),
      num[a[i]]++;
      
    for(int i=2;i<=maxa;i++)//枚举两根相等的边 
      if(num[i]>=2)
        {
          int times = C(num[i],2)%mod;//求出组合数 
          for(int j=1;j<=i/2;j++)//枚举被合成的边(枚举到i/2即可)
            {
              if(j!=i-j && num[j]>=1 && num[i-j]>=1)//当用来合成的木棒长度不等 
                ans=(ans + times*C(num[j],1)%mod*C(num[i-j],1)%mod)%mod;
              if(j==i-j && num[j]>=2)//当用来合成的木棒长度相等 
                ans=(ans + times*C(num[j],2))%mod;
            }
        }
    
    printf("%lld",ans%mod);
}

This complete solution to a problem, the car the much faith \ (+ \)


Guess you like

Origin www.cnblogs.com/luckyblock/p/11456240.html