2025. Line Fighting
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Boxing, karate, sambo… The audience is sick of classic combat sports. That is why a popular sports channel launches a new competition format based on the traditional Russian entertainment called line fighting. There can be from 2 to
k teams taking part in a competition, and there are
n fighters altogether in all the teams. Before the competition starts, the fighters are divided into teams: each fighter becomes a member of exactly one team. Two fighters fight each other if they are members of different teams. The organizers believe that the more the number of fights between fighters, the higher the popularity of a competition will be. Help the organizers to distribute fighters between teams so as to maximize the number of fights and output this number.
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.
Sample
input | output |
---|---|
3 6 3 5 5 4 2 | 12 10 4 |
Problem Author: Alexey Danilyuk
Problem Source: Ural Regional School Programming Contest 2014
Problem Source: Ural Regional School Programming Contest 2014
Tags: none
)
Difficulty: 98
Printable version
Submit solution
Discussion (5)
My submissions All submissions (3666) All accepted submissions (1651) Solutions rating (1010
My submissions All submissions (3666) All accepted submissions (1651) Solutions rating (1010
题意
n个人分成k组,不同组之间的人互相打一架,问怎么分组才能使打架次数最多,求最多的次数。
解题思路
跟握手定理很像,只不过同组的人之间不能打架,那么先按照握手定理做,然后再减去同组内打架的次数就可以了。
由于我们是要求结果最大,所以希望减的次数越少越好,也就是每组人之间打架次数越少越好,
每组之间大家次数是(1号组内成员数)*(1号组内成员数-1)/2+(2号组内成员数)*(2号组内成员数-1)/2+(3号组内成员数)*(3号组内成员数-1)/2........,
而所有组内成员数相加=n,所以分组越平均,上式越小。
于是我们将组平分,当n/k不能除开的时候,有 n%k 组 人数是n/k+1,有n-n%k组 人数是n/k
代码
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n,k;
scanf("%d%d",&n,&k);
if(n%k==0){
int ans = n*(n-1)/2;
int tn = n/k;
ans -= k*tn*(tn-1)/2;
printf("%d\n",ans);
}
else{
int ans = n*(n-1)/2;
int tn = n/k;///每组有tn个人
int x = n%k;///有x组人数不是tn而是tn+1
ans -= x*(tn+1)*(tn)/2;///x组每组tn+1人内部的次数
ans -=(k-x)*tn*(tn-1)/2;///k-x组每组tn人内部的次数
printf("%d\n",ans);
}
}
return 0;
}