数学(错排):BZOJ 4517: [Sdoi2016]排列计数
2016-06-12 19:24
579 查看
4517: [Sdoi2016]排列计数
Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 693 Solved: 434
[Submit][Status][Discuss]
Description
求有多少种长度为 n 的序列 A,满足以下条件:1 ~ n 这 n 个数在序列中各出现了一次
若第 i 个数 A[i] 的值为 i,则称 i 是稳定的。序列恰好有 m 个数是稳定的
满足条件的序列可能很多,序列数对 10^9+7 取模。
Input
第一行一个数 T,表示有 T 组数据。接下来 T 行,每行两个整数 n、m。
T=500000,n≤1000000,m≤1000000
Output
输出 T 行,每行一个数,表示求出的序列数Sample Input
51 0
1 1
5 2
100 50
10000 5000
Sample Output
01
20
578028887
60695423
错排还是很简单的……
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=1000010; const long long mod=1000000007LL; long long f[maxn],fac[maxn]; long long Inv(int x){ return x==1?1:(mod-mod/x)*Inv(mod%x)%mod; } int main(){ #ifndef ONLINE_JUDGE freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); #endif fac[0]=1;f[0]=1;f[1]=0; for(int i=1;i<=1000000;i++)fac[i]=fac[i-1]*i%mod; for(int i=2;i<=1000000;i++)f[i]=(i-1)*(f[i-1]+f[i-2])%mod; int T,n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); printf("%d\n",f[n-m]*fac %mod*Inv(fac[m])%mod*Inv(fac[n-m])%mod); } return 0; }
相关文章推荐
- Javaworkers团队第五周项目总结
- 月月月月月月月月
- [javaEE] response实现图片下载
- windows程序设计(2)---窗口的创建
- Qt浅谈之二十七进程间通信之QtDBus
- Two Sum
- Activity生命周期、启动方式及常见Activity
- Android入门--style.xml设置和应用
- linux给用户添加sudo权限
- DIV与CSS的结合使用
- <Js>运算
- java用正则方法验证文件名是否合法
- ADO.NET基础巩固-----连接类和非连接类
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- 模拟简单距离向量算法的更新——计算机网络作业
- Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频
- 第十六周--阅读程序2
- c primer plus第4章总结:字符串和格式化输入输出
- 初学java制作了一个绘图板
- “进度条”博客——第十五周