拼木棒

题目

题目

数据规模

在这里插入图片描述

讲解

大佬讲解:链接

代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 50005;
const ll mod = 1e9+7;
int n,a[maxn] = {
    
    0},t,allmax =0;
ll ans = 0;

//从K个里面挑选两个 
int C(int k){
    
    
	return k*(k-1)/2%mod;
}

int main(){
    
    
	cin>>n;
	//a数组中存放下标值的个数 
	for(int i = 0;i< n;i++){
    
    
		cin>>t;
		a[t]++;
		allmax = max(t,allmax);
	} 
	
	//循环两个较短的木棒,为了避免重复计算,设置j大于i 
	for(int i = 1;i<= allmax;i++){
    
    
		for(int j = i;j<= allmax;j++){
    
    
			if(i == j){
    
    
				ans= (ans + (C(a[i])*C(a[2*i]))) % mod;
			}
			else{
    
    
				ans = (ans + (a[i]*a[j])%mod *C(a[i+j]) ) % mod;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_45210226/article/details/108190744
今日推荐