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;
}

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

  1. URAL 1796. Amusement Park (math)

    1796. Amusement Park Time limit: 1.0 second Memory limit: 64 MB On a sunny Sunday, a group of childr ...

  2. 组合数学(math)

    组合数学(math) 题目描述 为了提高智商,zjy开始学习组合数学.某一天她解决了这样一个问题:“给一个网格图,其中某些格子有财宝.每次从左上角出发,只能往右或下走.问至少要走几次才能把财宝全部捡完 ...

  3. URAL 1069 Prufer Code(模拟)

    Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) ...

  4. URAL 1741 Communication Fiend(最短路径)

    Description Kolya has returned from a summer camp and now he's a real communication fiend. He spends ...

  5. URAL 1139 City Blocks(数论)

    The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...

  6. Linux Command Line 笔记(1)

    Yunduan CUI graphical user interfaces make easy tasks easy, while command line interfaces make diffi ...

  7. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  8. ural 1100. Final Standings(数据结构)

    1100. Final Standings Time limit: 1.0 secondMemory limit: 16 MB Old contest software uses bubble sor ...

  9. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

随机推荐

  1. Unity 两张背景的切换平移

    两张背景图片向左移动,当屏幕看见的时候. 使用的是Unity自带的Sprite,当然也可以使用NGUI Sprite using UnityEngine; using System.Collectio ...

  2. iOS加密个人见解

    说说常用的加密方式 1.单向加密,譬如 md5 .SHA 但是这种单向加密安全性也不高了,现在cpu.gpu都那么强大,运算速度很快,彩虹表 撞库 还是容易被攻破的. 如果非得用的话,可以md5加盐, ...

  3. 获取某月第一天,最后一天的sql server脚本

    本文来自:http://blog.csdn.net/chaoowang/article/details/9167969 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, ...

  4. Hide a file in a picture

    有时候.假设你想在电脑上隐藏关键的文件而不想让其它人看见.你会怎么做呢?找一个专业的工具?为目录设置password?更改文件属性?这些方法可行.但它们可能不太方便和安全.这里,我给大家共享一个在图片 ...

  5. 算法设计手冊(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008

    The Algorithm Design Manual, 2ed 跳转至: 导航. 搜索 Springer - The Algorithm Design Manual, 2ed Steven S.Sk ...

  6. MySQL中innodb表主键设计原则

    主键设计的原则:1. 一定要显式定义主键2. 采用与业务无关的单独列3. 采用自增列4. 数据类型采用int,并尽可能小,能用tinyint就不用int,能用int就不用bigint5. 将主键放在表 ...

  7. Linux下JDK的安装

    1.首先到官网上下载 jdk-7u7-linux-x64.tar.gz 2.将jdk-7u7-linux-x64.tar.gz拷贝到/usr/lib/jdk/目录下面,这里如果没有jdk文件夹,则创建 ...

  8. 浅谈JavaScript DOM编程艺术读后感和一点总结

    最近工作不是很忙就想想想JavaScript的内部组成和一些要点,就是从这本书开始的.对新手来说还好,简单易懂. 简单终结下我重书中学到的一些要点. 下面都是个人学习的要点提取: 1.给自己预留退路, ...

  9. 【Remoting-5代码实现】

    服务端 class RemotingServiceHelper { private static string m_protocolType; private static string urlStr ...

  10. HDU 1070 - Milk

    给每种牛奶价格和量 要求买最便宜的牛奶 #include <iostream> using namespace std; int t,n; ][]; ],v[]; int main() { ...