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; }
相关文章推荐
- Open Module Setting无法打开
- 关于java中的本地缓存-总结概述
- ava.lang.VerifyError 解决方案(Android studio版版更新出现的问题)
- 静态编译动态编译;静态链接动态链接;静态库和动态库
- 【转】线程状态
- [置顶] SecureCRT 上应用Linux命令在Linux系统服务器中发布项目
- linux ps指令
- Runtime(三)方法与消息
- bash功能 命令别名与历史记录
- JAVA实现DES加密
- ubuntu中安装R及Rstudio
- 51822 --- 主机主动扫描和被动扫描
- qt信号阻塞blockSignals的运用
- 函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义
- 关于本地存储localForage
- Node.js刷新session过期时间的实现方法推荐
- [拦截器]关于拦截方法调用其他内部方法无法被拦截问题的解决
- struts1.x国际化
- 正则表达式积累
- GCD工作单元