最近挺傻逼的
题意:
把N个元素分成K个组,定义两个元素不属于同一组则他们之间有一次Fight,问最多能有多少Fight?
Input
The first line contains the number of tests
T (1 ≤
T ≤ 10). In each of the following
T lines you are given a test: integers
n and
k separated with a space (2 ≤
k ≤
n ≤ 10
4).
Output
For each test output the answer (one integer) in a separate line.
最优分组法是每组人数尽可能平均,这不难猜到,猜到之后代码也就很简单。
这里用数学归纳法证明一下:
基础情况就是(a+b)=c,a*b最大,就是2*sqrt(ab)<=a+b的均值不等式
递推如下:
第n次结论是a0*(a1+a2+a3+...+an)+a1*(a2+a3+...+an)+...+an-1*an当a0+...+an和为定值时,a值全部相等该代数式最大。
第n+1次的代数式可写成上式+a0*an+1+a1*an+2+...+an*an+2,运用均值不等式即可证明an+1=a0=a1=...=an时n+1式最大
然后题目具体情况有可能是不能整除的,但是均值不等式的函数是在最值点左右单调的,只要最接近即可
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 10010
int a[mxn];
int n,k;
int main(){
int cs;
scanf("%d",&cs);
while(cs--){
scanf("%d%d",&n,&k);
int tem=n/k;
for(int i=0;i<n;++i)
a[i]=tem;
tem=n-k*tem;
for(int i=0;i<n;++i){
if(!tem) break;
++a[i];
--tem;
}
long long ans=0;
tem=n-a[0];
for(int i=0;i<k;++i){
ans+=a[i]*tem;
tem-= i==n-1 ? 0 : a[i+1];
}
printf("%lld\n",ans);
}
return 0;
}