【Codeforces Round 273 (Div 2)B】【贪心】 Random Teams n人分m组,可形成的最小朋友数和最大朋友数

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




【Codeforces Round 273 (Div 2)B】【贪心】 Random Teams n人分m组,可形成的最小朋友数和最大朋友数

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int n,m;
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		LL num0=n/m;
		LL team0=m-n%m;
		LL num1=num0+1;
		LL team1=n%m;
		LL minv=team0*num0*(num0-1)/2+team1*num1*(num1-1)/2;

		LL num=(n-m+1);
		LL maxv=num*(num-1)/2;
		
		printf("%lld %lld\n",minv,maxv);
	}
	return 0;
}
/*
【题意】
给你n和m,有1<=m<=n<=1e9,表示有n个人要分到m组当中去。
每个组至少要分一人,每个组内的每两个人都会成为朋友。
问你如何分组,可以得到最小朋友对数和最大朋友对数,并输出。

【类型】
贪心

【分析】
显然——
最小朋友对数,取自分配最平均的情况
最大朋友对数,取自分配最极端的情况
依次算出来即可。

【时间复杂度&&优化】
O(1)

*/


你可能感兴趣的:(codeforces,贪心,题库-CF)