(错排)杭电题 2068 RPG的错排

RPG的错排

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2068
思路:就是错排,从n个女生里抽出m(m<(n>>1))个排在错误位置上的女生.

#include<iostream>
using namespace std;
typedef long long ll;
ll c[30] = {
    
    0,0,1};//排错
ll dg(int n, int m) {
    
    
	ll temp = 1;
	for (int i = 0; i < m; i++) {
    
    
		temp = temp * (n - i) / (i + 1);//Cn-m;
	}
	return temp;
}
int main() {
    
    
	int n;
	for (int i = 3; i <= 25; i++)
		c[i] = (i - 1) * (c[i - 2] + c[i - 1]);
	
	while (cin >> n&&n) {
    
    
		ll sum = 0;//这里我放外面去了,导致发生了低级错误
		for (int i = 0; i <= (n >> 1); i++)
			sum += dg(n, i)*c[i];
		printf("%lld\n", sum+1);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46028214/article/details/115188047