【HDOJ】2068 RPG的错排
其实就是排列组合+错排,得自学组合数学了。
1 #include <stdio.h> 2 3 #define MAXNUM 26 4 5 __int64 nums[MAXNUM] = {0,0,1}; 6 7 int main() { 8 int n; 9 int i, j; 10 __int64 tmp, k, res; 11 12 for (i=3; i<MAXNUM; ++i) { 13 nums[i] = (i-1) * (nums[i-1]+nums[i-2]); 14 } 15 while (scanf("%d", &n)!=EOF && n) { 16 res = 1; 17 for (i=2; i+i<=n; ++i) { 18 // C^i_n 19 k = tmp = 1; 20 for (j=1; j<=i; ++j) 21 k *= j; 22 for (j=n; j>=n-i+1; --j) 23 tmp *= j; 24 res += tmp/k*nums[i]; 25 } 26 printf("%I64d\n", res); 27 } 28 29 return 0; 30 }