UVa:11481 Arrange the Numbers
2014-01-16 15:02
387 查看
这个题应该也可以用递推来做(LRJ白书上这么写的,但是我不会),我用了容斥原理。
公式是ans=C(m,k)*【(n-k)!-C(m-k,1)*(n-k-1)!+C(m-k,2)*(n-k-2)!-……+(-1)^i*C(m-k,i)*(n-k-i)!】其中i的范围是【0,m-k】
这个公式用排除法很好想到,但是我却因为不会求组合数不会模运算折腾了好长时间。。。
公式是ans=C(m,k)*【(n-k)!-C(m-k,1)*(n-k-1)!+C(m-k,2)*(n-k-2)!-……+(-1)^i*C(m-k,i)*(n-k-i)!】其中i的范围是【0,m-k】
这个公式用排除法很好想到,但是我却因为不会求组合数不会模运算折腾了好长时间。。。
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define MAXN 1005 #define MOD 1000000007 #define INF 2139062143 #define EPS (1e-8) #define ll long long using namespace std; ll C[MAXN][MAXN],Fac[MAXN]; void Init() { Fac[0]=1; for(int i=1; i<=1000; ++i) { ll res=Fac[i-1]*i; Fac[i]=res%MOD; } for(int i=0; i<=1000; ++i) for(int j=0; j<=1000; ++j) C[i][j]=1; for(int i=0; i<=1000; i++) { C[i][0]=C[i][i]=1; for(int j=1; j<i; j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD; } } int main() { Init(); int T,kase=0; scanf("%d",&T); while(T--) { int m,n,k; scanf("%d%d%d",&n,&m,&k); ll ans=Fac[n-k]; for(int i=1; i<=m-k; ++i) { ll res=C[m-k][i]*Fac[n-k-i]%MOD; if(i%2) ans=ans-res; else ans=ans+res; ans=ans%MOD; if(ans<0) ans=ans+MOD; } ans=ans*C[m][k]%MOD; printf("Case %d: %lld\n",++kase,ans); } return 0; }
相关文章推荐
- UVA 11481 Arrange the Numbers (容斥)
- uva11481 - Arrange the Numbers N个数前M个错排
- uva 11481 - Arrange the Numbers(计数问题)
- uva 11481 Arrange the Numbers
- UVA - 11481 Arrange the Numbers
- uva11481 Arrange the Numbers(错位排列)
- UVa 11481 Arrange the Number(容斥原理)
- lightoj 1095 - Arrange the Numbers 组合数学
- LightOJ-1095 Arrange the Numbers
- LightOJ 1095 Arrange the Numbers(容斥原理)
- UVa 11481 (计数) Arrange the Numbers
- UVa 913 - Joana and the Odd Numbers
- Light OJ 1095 Arrange the Numbers(容斥)
- UVa 10712 - Count the Numbers
- Light oj 1095 - Arrange the Numbers(错排)
- Light OJ 1095 Arrange the Numbers (容斥)
- UVA913 Joana and the Odd Numbers【数学】
- Light OJ 1095 Arrange the Numbers(计数)
- LightOJ 1095 Arrange the Numbers-容斥
- uva 11471 Arrange the Tiles (DP)