【Luogu P3799】Demon Dream Fighting Wooden Stick 【Combination Mathematics】

Insert picture description here


Problem solving ideas

To put it bluntly, choose two equal sticks, and then pick the two that add up to the original two sticks, and combine them.
****
Because mmm is equal to2 2in this question2 , then we can simplify it to get:2 / n (n − 1) 2/n(n−1)2/n(n1)


Code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const long long INF=1000000007;
long long n,a[100010],maxn,ans,x;

int main(){
    
    
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	{
    
    
		scanf("%lld",&x);
		a[x]++;
		maxn=max(maxn,x);
	}	
	for(int i=2;i<=maxn;i++)
	{
    
    
		if(a[i]>1)
		{
    
    
			for(int j=1;j<=i/2;j++)
			{
    
    
				if((i-j)!=j&&a[j]&&a[i-j])
					ans=(ans+(((a[i]*(a[i]-1)/2)*a[j]*a[i-j]))%INF)%INF;
				if((i-j)==j&&a[j]>1)
					ans=(ans+(((a[i]*(a[i]-1)/2)*(a[j]*(a[j]-1)/2)))%INF)%INF;
			}
		}
	}
	printf("%lld",ans);
}

Guess you like

Origin blog.csdn.net/kejin2019/article/details/111406691