Codeforces-273(div2) B. Random Teams

来源:互联网 发布:软件系统架构ppt 编辑:程序博客网 时间:2024/04/20 01:03
B. Random Teams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
5 1
output
10 10
input
3 2
output
1 1
input
6 3
output
3 6
Note

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 2 people, maximum number can be achieved if participants were split on teams of 11 and 4 people.

题意:n个人分成m个队进行比赛,比赛完之后每个队里人成为朋友,现在让我们求最少有多少对朋友,最少有多少对朋友。

做法:假设一个队里有x个人,那么所产生的朋友对数就是x-1+x-2+...+1,也就是(x-1+1)*(x-1)/2;要使朋友对数最多,那么最优的解决方案应该是(m-1)队都是1人一队,然后剩下的(n-(m-1))人为一队;使朋友对数最少,就要让n个人平摊到m个队伍上,所以有(res=n-n/m*m)个队伍是(n/m+1)个人,(m-res)个队伍是(n/m)个人,最后分别算出最大值和最小值就是答案。

#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include<map>#include <algorithm>#include<ctime>#define esp 1e-6#define LL unsigned long long#define inf 0x0f0f0f0fusing namespace std;int main(){__int64 n,m,min1,max1,tt,kk;__int64 sl,mm,res;while(scanf("%I64d%I64d",&n,&m)!=EOF){tt=n;kk=m;res=0;min1=max1=0;if(n%m==0){mm=n/m;min1=m*(mm*(mm-1)/2);}else{mm=n/m;res=n-n/m*m;min1=res*((mm+1)*(mm)/2);min1+=(m-res)*(mm*(mm-1)/2);}n=tt;m=kk;max1=((n-(m-1))*((n-(m-1))-1)/2);printf("%I64d %I64d\n",min1,max1);}return 0;}


0 0