您的位置:首页 > 其它

hdu 3010 N Knight 组合数学

2016-05-18 17:10 435 查看


网上还没有这个题的题解,就来写一个。排在第一,开心~~

首先,这个对角线无论是主对角还是副对角,结果都是一样的,所以把题目要求的副对角改成主对角。

然后枚举对角线上摆放棋子的个数 0 <= i <= m, 这个取的方案数显然为 C(n,i).

剩下n-i个棋子的摆放都不能在这条对角线上,把每个行标号看成球,列标号看成盒子。放对角线上就相当于,把球放进自己对应的盒子。

而现在要求不在对角线,相当于把n-i个球放入自己不对应的n-i个箱子。这个问题的方案数显然是可以预处理的。 假设这个方案数是cp[n-i]

则答案就是 sigma(C(n,i)*cp[n-i]) 0 <= i <= m

然后就是求cp 的问题。 这其实就是一个错排问题。有递推公式 cp[i] = (i-1)*(cp[i-1] + cp[i-2])

证明见百度百科:http://baike.baidu.com/link?url=FNwD0IgZ9psNpdtw8sLz-exb9RjDLu9iqQCEfabA_gOzOVwuIQR0kdTs2o7kHxXo0GMuSVJdWj1uh3yuq9XSU_

(第一的代码藏起来了,这里给一个最初写的代码)

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
void fre(){freopen("t.txt","r",stdin);}
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(y))
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
const int INF = 0x3f3f3f3f ;
const double pi = acos(-1.0);
const int MAXN = 100010;
const LL M = 20090818;

LL C[1010][1010],cp[1010];
void init()
{
for(int i = 0; i <= 1000; ++i) C[i][0] = C[i][i] = 1;
for(int i = 2; i <= 1000; ++i)
for(int j = 1; j < i; ++j) C[i][j] = (C[i-1][j] + C[i-1][j-1])%M;
cp[0] = cp[2] = 1;
for(int i = 3; i <= 1000; ++i) cp[i] = (cp[i-1]+cp[i-2])%M*(i-1)%M;
}
int n,m;
void solve()
{
LL ans = 0;
for(int i = 0; i <= m; ++i)
ans = (ans + C
[i]*cp[n-i])%M;
printf("%lld\n",ans);
}
int main()
{
init();
while(~scanf("%d%d",&n,&m))
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: