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 ≤ 104).

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

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

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

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;
}
时间: 2024-10-14 00:39:14

URAL 2025. Line Fighting (math)的相关文章

URAL 1796. Amusement Park (math)

1796. Amusement Park Time limit: 1.0 second Memory limit: 64 MB On a sunny Sunday, a group of children headed by their teacher came to an amusement park. Aunt Frosya,who was a very kind and quiet person, worked at the ticket window on that day. The t

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[

URAL 1167. Bicolored Horses (DP)

题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所有的马都必须入马厩.有两种颜色的马,如果 i 匹黑马与 j 匹白马同在一个马厩,不愉快系数是 i * j,总系数就是k个系数相加.让总系数最小. 思路 : dp[i][j] 代表的是前 i 个马厩放 j 匹马的最小不愉快系数值. 1 //1167 2 #include <cstdio> 3 #in

URAL 1306 Sequence Median(优先队列)

题意:求一串数字里的中位数.内存为1M.每个数范围是0到2的31次方-1. 思路:很容易想到把数字全部读入,然后排序,但是会超内存.用计数排序但是数又太大.由于我们只需要第n/2.n/2+1大(n为偶数)或第(n+1)/2大(n为奇数).所以可以用优先队列来维护最值,这样只需要存一半元素(n/2+1个元素)就可以了. #include<cstdio> #include<algorithm> #include<queue> #define UL unsigned int

poj3623 Best Cow Line, Gold(贪心)

题目链接: huangjing 思路: 选取字典序最小的串,那么值得考虑的是当两端出现相等时,继续比较,直到出现不同的结果.. 题目: Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4574   Accepted: 1682 Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farme

【HDU 3400】Line belt(三分法)

题目链接 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3400 题意 有两条传送带AB和CD,移动速度分别为p,q. 除了传送带的其他区域移动速度为r,问A到D最短时间. 题目分析 在AB上找一点E,在CD上找一点F. 使得A->E->F->D时间最短. 数学思路 时间 time = |AE|/p + |EF|/r + |FD|/q. (|AE|为线段AE的长度.) 未知量有E的位置和F的位置,由于确定在AB和CD上,所以只需要两个未知量

Linux Command Line 笔记(1)

Yunduan CUI graphical user interfaces make easy tasks easy, while command line interfaces make difficult tasks possible Part 1 学习Shell 1. 什么是 Shell? Shell 是用户与操作系统交流的程序,它读取用户的键盘输入并交由操作系统执行相应的命令.所有linux都支持一个叫做 bash 的shell,它的全称是 "Bourne Again SHell&quo

URAL 1196. History Exam (二分)

1196. History Exam Time limit: 1.5 second Memory limit: 64 MB Professor of history decided to simplify the examination process. At the exam, every student should write a list of historic dates she knows (she should write the years only and, of course

Ural 1780 Gray Code (暴力)

Ural 1780 题意: 输入一个二进制数与该二进制数的格雷码,但有一些位置不确定,写程序将其恢复,无法恢复输出Impossible,多解输出Ambiguity. 思路: 其实是个普通的乱搞题.. Gray码的定义:Gi=Bi?1?Bi,G0=B0 (第i位格雷码等于第i位二进制码与第i-1位二进制码的异或值) 然后按照这个定义编码,正向反向各自编一遍,出现矛盾输出Impossible,最后还含有'?'则有多解. 代码: /* * @author FreeWifi_novicer * lang