2025. Line Fighting
Time limit: 1.0 second
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 ≤ 104).
Output
For each test output the answer (one integer) in a separate line.
Sample
input
3
6 3
5 5
4 2
output
12
10
4
Problem Author: Alexey Danilyuk
Problem Source: Ural Regional School Programming Contest 2014
Tags: none (hide tags for unsolved problems)
tho: 对于一种情况的方案解决策略如下
最终结果:
1.如果人数可以平均分到每只队伍中,则
ans=(一只队伍的人数×剩下的所有的人数×队伍总数)/2。【除2原因是高中知识点】
即从一个队中选出一人比赛有 这支队伍的人数 这么多种选择的可能性,而他又可以和其余队伍中任何选手比赛 所以
2.若不能均分,则要尽量平均分配
举个栗子: 8 5
(12) (34) (56) (7) (8)
这样分得到可以举办的比赛数目最多
此时 n/k = 1 n%k = 3
现在答案的计算可以分为两部分
①这部分是人少的队伍计算出的答案
[(n/k) × (n- n/k) × (k - n%k)] / 2
队中1人 剩余7人 这种队伍有(5-3)两只
②这部分是人多的队伍计算出的答案
[(n/k + 1) × (n- n/k -1) × (n%k)] / 2
队中2人 剩余6人 这种队伍有3只
**因为总数除以二了所以不会出现重复的**
推演后发现,第二种情况导出的式子包含第一种情况。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
LL n,k;
scanf("%lld%lld",&n,&k);
LL chu = n/k;
LL mo = n%k;
LL lesss = chu*(n-chu)*(k-mo);
LL moree = (chu+1)*(n-chu-1)*mo;
printf("%lld\n",(lesss+moree)/2);
}
return 0;
}