URAL 2025. Line Fighting (math)

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 ≤ kn ≤ 10 4).

Output

For each test output the answer (one integer) in a separate line.

Sample

inputoutput
3
6 3
5 5
4 2
12
10
4
Problem Author: Alexey Danilyuk
Problem Source: Ural Regional School Programming Contest 2014




解析:组合数学。因为组内不能打比赛,这就相当于在全部人都能比赛的基础上去掉了各个组间能打的比赛次数。

首先,比赛次数最多的情况肯定是尽可能地将人数均分,这种比赛数是最多的。



AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int T, n, k, ans;
    scanf("%d", &T);
    while(T --){
        scanf("%d%d", &n, &k);
        ans = n * (n - 1) / 2;     //全部人两两之间打比赛的次数
        if(n != k){
            int foo = n / k;
            int cnt = n % k;       //均分后剩余cnt个人,再均分。则会出现cnt个人数多1的组
            ans -= cnt * ((foo + 1) * foo / 2);   //去掉人数较多的cnt组的总次数
            ans -= (k - cnt) * (foo * (foo - 1) / 2);   //去掉人数较少的总次数
        }
        printf("%d\n", ans);
    }
    return 0;
}


转载于:https://www.cnblogs.com/lcchuguo/p/5222728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值