暴力的时间复杂度是O(N!)不废话,稍微发现,当出现小于号的时候后面的结构都是一样的,所以使用记忆化方式求,时间复杂度是O(2^n),对于本题的测试数据量50,还是压力非常大的,考虑如下序列,当序列第一次出现小于号的时候,其实就是组合数, 设F[n] = sum(Cn,k*F[n-k]) (1 <= k <= n) import java.util.*; import java.math.*; class Main { static int MAXN = 51; static BigInteger[] f = new BigInteger[MAXN]; static void cal(int n) { BigInteger up, down; f[0] = BigInteger.valueOf(0x1); for(int i = 1; i <= n; i ++) { f[i] = BigInteger.valueOf(0x0); for(int k = 1; k <= i; k ++) { down = up = BigInteger.valueOf(0x1); for(int j = 1; j <= i; j ++) { up = up.multiply(BigInteger.valueOf(j)); } for(int j = 1; j <= k; j ++) { down = down.multiply(BigInteger.valueOf(j)); } for(int j = 1; j <= i-k; j ++) { down = down.multiply(BigInteger.valueOf(j)); } up = up.divide(down); f[i] = f[i].add(up.multiply(f[i-k])); } } } public static void main(String [] args) { for(int i = 1; i < MAXN; i ++) { cal(i); } Scanner scanf = new Scanner(System.in); int cas, n; cas = scanf.nextInt(); for(int i = 0; i < cas; i ++) { n = scanf.nextInt(); System.out.println(f[n]); } } }
hdu_1223 Order Count
最新推荐文章于 2020-08-15 10:56:42 发布