51nod 算法马拉松30 A.函数【容斥】【组合数学】
2017-10-28 20:02
363 查看
Description
想知道f:A->B这个函数(其中|A|=n, |B|=m)的所有映射关系要使B的每个元素都要被A的一个元素覆盖到。数字可能很大你只要输出方案数模1,000,000,007即可。
题解
枚举有在B中有几个元素一定被映射到了,假设B中有i个元素一定被映射到了,那么方案就是Cim∗(m−i)n,考虑容斥来统计答案,所以答案就是∑mi=0Cin∗(m−i)n∗(−1)i。代码
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 1000006 #define tt 1000000007 #define LL long long using namespace std; int n,m; LL ans,f[maxn],inv[maxn]; LL power(LL x,int y){ if(!y)return 1; if(y==1)return x%tt; LL c=power(x,y>>1); if(y&1)return c*x%tt*c%tt; else return c*c%tt; } LL C(int x,int y){return f[x]*inv[y]%tt*inv[x-y]%tt;} int main(){ freopen("function.in","r",stdin); freopen("function.out","w",stdout); scanf("%d%d",&n,&m); if(n<m)return printf("0\n"),0; f[0]=inv[0]=1; for(int i=1;i<=m;i++)(f[i]=f[i-1]*i)%=tt; inv[m]=power(f[m],tt-2); for(int i=m-1;i>=1;i--)inv[i]=inv[i+1]*(i+1)%tt; for(int i=0;i<m;i++)(ans+=C(m,i)*power(m-i,n)%tt*((i&1)?-1:1))%=tt; printf("%lld\n",(ans+tt)%tt); return 0; }
相关文章推荐
- [排列组合 + 分段打表] 51nod 算法马拉松25 A. 二分答案
- [数学] 51Nod 1622 算法马拉松19 C 集合对
- 51Nod-算法马拉松23-B-谷歌的恐龙(数学期望)
- 51Nod-算法马拉松23 B 谷歌的恐龙 [概率期望]【数学】
- 51nod 算法马拉松3 A:序列分解
- 51nod 1394 差和问题(算法马拉松8)
- 【算法总结】组合数学相关
- 51nod 1118 机器人走方格(组合数学)
- [cf803F] Coprime Subsequences(组合数学,容斥)
- 【算法】组合数学——排列数生成算法详解(一)
- 51Nod 算法马拉松7 B选数字
- 51nod 算法马拉松4
- 【51Nod 1616】【算法马拉松 19B】最小集合
- 51Nod 算法马拉松24
- 51nod 算法马拉松25
- 51nod 算法马拉松 天堂里的游戏
- 51Nod 算法马拉松21(迎新年)
- C/C++面试之算法系列--如何利用数学思想解1/2/5组合问题
- 组合数学——排列数生成算法详解(zz)
- 51NOD 1806 wangyurzee的树(容斥原理 + 组合数学)