题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2200
总人数为n, 则每次选中的人数为2--n。
若选中人数为2 ,则这两个人的ac数只有一种分组方式。 从n个人中选2个人,共有C(n,2)中可能。 --------- 1*C(n,2)
若选中人数为3 ,则这3个人的ac数只有2种分组方式。 从n个人中选3个人,共有C(n,3)中可能。 --------- 2*C(n,3)
若选中人数为4 ,则这4个人的ac数只有3种分组方式。 从n个人中选4个人,共有C(n,4)中可能。 --------- 3*C(n,4)
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
若选中人数为n ,则这n个人的ac数有n-1种分组方式。 从n个人中选n个人,共有C(n,n)中可能。 --------- (n-1)*C(n,n)
将右侧的结果相加即为最终结果。注意:数据类型使用64位的double
java代码如下:
import java.io.BufferedInputStream;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main
{
public static double result(double n, double i)
{
double result = 1;
for(double j=i; j>=1; j--)
{
result = result * n/j;
n--;
}
return result;
}
public static void main(String[] args)
{
Scanner sin = new Scanner(new BufferedInputStream(System.in));
while(sin.hasNext())
{
double count = sin.nextLong();
double resultFinal = 0;
for(int i=2; i<=count; i++)
{
resultFinal += (i-1) * result(count,i);
}
DecimalFormat df = new DecimalFormat("0");
System.out.println(df.format(resultFinal));
}
sin.close();
}
}