URAL 2025. Line Fighting (math)
2015-06-24 14:46
351 查看
2025. Line Fighting
Time limit: 1.0 secondMemory 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 | output |
---|---|
3 6 3 5 5 4 2 | 12 10 4 |
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; }
相关文章推荐
- codeforces #306 E 550E E. Brackets in Implications(构造)
- wp7的web使用
- javascript window.opener的用法分析
- jquery实现从数组移除指定的值
- 浅析PL/SQL过程化语言编程要点
- 企业选购邮件服务器五大指标
- 刘姝威以已之短隔山打牛,乐视几近涨停
- Razor与HTML混合输出陷阱与技巧
- jQuery中的join方法
- 【转载】华为绩效管理:这样减员、增效、加薪,不服不行!
- linux下eclipse交叉开发环境搭建
- 分享图片+文字到微信朋友圈
- MVC4 razor与aspx的区别以及用法
- wxWidgets之wxGrid控件
- Objective-C编码规范[译]
- 四象限管理法
- 对Objective-C中Block的追探
- 作业6--再次冲刺即最终冲刺
- Swift语言学习(四)字符串与Array等集合的使用
- 安卓开发2-质量管理app-UI设计与实现