Description
n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.
Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.
Input
The only line of input contains two integers n and m, separated by a single space (1 ≤ m ≤ n ≤ 109) — the number of participants and the number of teams respectively.
Output
The only line of the output should contain two integers kmin and kmax — the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.
Sample Input
Input
5 1
Output
10 10
Input
3 2
Output
1 1
Input
6 3
Output
3 6
Hint
In the first sample all the participants get into one team, so there will be exactly ten pairs of friends.
In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.
In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people.
题意:有n个选手和m个队伍,让你分配,条件是每个队伍至少要有1个选手。分配完之后,每队伍里2个人可以组成一组,求分配完之后最多的组数和最少的组数....
题解:只要考虑怎么分最多,怎么分最少就好了
1. 最多的情况就是,先每个队伍分一个人,然后把剩下的全部给到一个队伍里,就是最多的情况 例如:n=10,m=3
1 1 8
2、 最少的情况就是,每队分n/m个人,然后剩下的再一个一个的分给每队。 例如
3 3 3+1
代码如下:
#include <stdio.h> int main() { int n,m; long long kmin=1,kmax=1; scanf("%d%d",&n,&m); int d=n-m+1; //因为把剩下的全部分到一队中的那只队伍里原来就存在一个人了,所以要加1 //printf("d=%d\n",d); for(int i=d; i>d-2; i--) //这里手贱 用了循环 其实直接就可以乘出来 这里相当于 在d人中中取两个出来(下面的是一样的) { kmax*=i; //printf("kmax=%d\n",kmax); } kmax/=2; int x=n/m; //每队均分到多少人 int s=n%m; //剩下的人 for(int i=x; i>x-2; i--) { kmin*=i; //printf("kmin=%d\n",kmin); } kmin/=2; kmin*=m; //因为是均分,所以一队的分组情况乘以m就是目前所有的 kmin+=s*x; //最后还要加上,剩下的人,每队给一个直到没有情况。 可以想一下,如果在原来的有x个人的队伍中在家上一个,就会多出来x中取两个的情况,所以这里还剩下s个人,就会多出s*x种 printf("%lld %lld\n",kmin,kmax); }