标签:
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
5 1
10 10
3 2
1 1
6 3
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 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people.
程序分析:
设第i个集合的元素个数为a[i]。那么它对答案的贡献为v[i]=a[i]*(a[i]-1)/2,所以最终答案为v[i]的和。而答案算式可化为:
(a[1]^2+a[2]^2+……+a[m]^2-n)/2。我们知道(a+b)^2>=a^2+b^2。所以大约可以猜出ansmax=(n-m+1)*(n-m)/2,即让一个集合尽量大。而ansmin则是每个集合个数大约相等。我要被这个题目坑死了,一直都是过不了。不会求min后面看了别人的代码才。。
程序代码:
#include<iostream> #include<cstdio> #define ll long long using namespace std; ll m,n,mina,maxa,rest; int main() { scanf("%I64d%I64d",&n,&m); maxa=(n-m+1)*(n-m)/2; rest=n-m*(n/m); mina=rest*(n/m+1)*(n/m)/2+(m-rest)*(n/m)*(n/m-1)/2; printf("%I64d %I64d\n",mina,maxa); return 0; }
标签:
原文地址:http://www.cnblogs.com/yilihua/p/4748935.html