ZOJ 1475 Ranklist

组合数学,递推

很多组合数的求解都是递推方法解的,多做些题目,涨涨姿势……

:i个人,排j个rank的组合数,则


:i-1个人把j个rank占满了,第i个人有j种rank,所以乘以j

:i-1个人把j-1个rank占满了,第i个人随便插入j-1个rank中的方案有j种

则,n个人的总方案数为:


组合数很大,用java大数给过的……

import java.util.*;
import java.math.*;

class Main {
	static public void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger[][] F = new BigInteger[201][201];
		BigInteger fac = BigInteger.ONE;
		for (int i=1; i<201; i++) {
			F[i][1] = BigInteger.ONE;
			fac = fac.multiply(BigInteger.valueOf(i));
			F[i][i] = fac;
		}
		for (int i=1; i<201; i++) {
			for (int j=2; j<i; j++) {
				F[i][j] = F[i-1][j].add(F[i-1][j-1]).multiply(BigInteger.valueOf(j));
			}
		}
		BigInteger sum;
		int n;
		while (cin.hasNextInt()) {
			n = cin.nextInt();
			if (n < 0) break;
			sum = BigInteger.ONE;
			for (int i=2; i<=n; i++) sum = sum.add(F[n][i]);
			System.out.println(sum);
		}
	}
}

话说,我又用python写了一个版本。呵呵。

F = [[1]*(i+1) for i in range(0, 201)]
fac = 1
for i in range(1, 201):
    fac = fac * i
    F[i][i] = fac
    for j in range(2, i):
        F[i][j] = (F[i-1][j] + F[i-1][j-1])*j
        
def S(n): return sum(x for x in F[n]) - 1

n = int(raw_input())
while n > 0:
    print S(n)
    n = int(raw_input())


你可能感兴趣的:(ZOJ 1475 Ranklist)