srm 711 DerangementsDiv2(dp)
来源:互联网 发布:天津淘宝模特 编辑:程序博客网 时间:2024/05/11 04:30
题意:
1-n+m的数排列,要求前m个数p[i]!=i,问有多少种排列方式,对1e9+7取模
解题思路:
不会做,最后问的p神才会了。
dp状态是dp[i][j],表示i个数排列要求前j个数 错排的方案数。
转移dp[i][j]=(i-j)*d[i-1][j]+j*dp[i-1][j-1];
分别表示最后一个数放在后n个位置还是前m个位置的两种方法。
i=j的时候就是错排公式,要单独写。
代码:
#include <bits/stdc++.h>#define LL long longusing namespace std;const int maxn=55;const int mod=1e9+7;LL fa[202];int i, j;LL dp[105][104];;void init(){ fa[0]=1; for(int i=1; i<=200; i++) { fa[i]=(fa[i-1]*(LL)i)%mod; } return;}struct DerangementsDiv2{ LL i, j; int count(int n, int m) { init(); memset(dp, 0, sizeof dp); dp[0][0]=1, dp[1][1]=0, dp[2][2]=1; for(i=3; i<=n+m; i++) { dp[i][i]=((i-1)*((dp[i-1][i-1]+dp[i-2][i-2]))%mod)%mod; } for(i=0; i<=n+m; i++) { dp[i][0]=fa[i]; if(i)dp[i][1]=((i-1)*fa[i-1])%mod; } for(i=1; i<=n+m; i++) { for(j=2; j<=m; j++) { if(i==j)continue; dp[i][j]=(((i-j)*(dp[i-1][j]))%mod+ (j*dp[i-1][j-1])%mod)%mod; } } return dp[n+m][m]; }};/*int main(){ DerangementsDiv2 a; int x, y; while(~scanf("%d %d", &x, &y)) a.count(x, y); }*/
阅读全文
0 0
- srm 711 DerangementsDiv2(dp)
- SRM 440 WickedTeacher (DP)
- SRM 599 D2L3: SimilarNames2,dp
- SRM 609 D2L3: VocaloidsAndSongs,dp
- SRM 602 D1L1:TypoCoderDiv1,dp
- SRM 602 D2L3:BlackBoxDiv2,dp
- SRM 604 D2L3:FoxConnection2,dp
- SRM 591 D2L3:YetAnotherTwoTeamsProblem,dp
- SRM 610 D2L3:MiningGoldEasy,dp
- SRM 574 D1L2:PolygonTraversal,dp
- SRM 612 D2L3:PowersOfTwo,dp
- SRM 615 D2L3:MergeStrings,dp
- SRM 615 D1L3: AlternativePiles, dp
- SRM 620 D2L3: RandomGraph, dp
- SRM 649 div2 500(dp)
- [DP套DP] SRM 591 div1 StringPath
- srm 556 div1 500 LeftRightDigitsGame2(DP)
- SRM 448 DIV 1 总结(dfs, dp)
- codevs 1021 玛丽卡 解题报告
- poj1850/poj1496 组合数
- webapp
- windows10(本机)与VirtualBox中CentOS7(虚拟机)互相访问总结
- 实习第一个月
- srm 711 DerangementsDiv2(dp)
- JBOSS7热部署-远程调试
- ZooKeeper使用(4)-- ZooKeeper的数据模型
- android安卓股票+聊天实现(一)
- android控件11---DrawerLayout
- altium designer PCB布线规则
- c#方法-虚方法
- win10 磁盘总是100%优化亲测
- 面试题 6: 重建二叉树