BZOJ 4517: [Sdoi2016]排列计数
4517: [Sdoi2016]排列计数
Time Limit: 60 Sec Memory Limit: 128 MB
Submit: 911 Solved: 566
[Submit][Status][Discuss]
Description
Input
Output
输出 T 行,每行一个数,表示求出的序列数
Sample Input
1 0
1 1
5 2
100 50
10000 5000
Sample Output
1
20
578028887
60695423
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 1000010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
#define md 1000000007 llg T,n,m;
llg D[maxn],N[maxn]; void make_D()//错排递推式 f(n)=(n-1)*[f(n-1)+f(n-2)]
{
D[]=,D[]=;
for (llg i=;i<=maxn-;i++) D[i]=(i-)*(D[i-]+D[i-]),D[i]%=md;
} void maken()
{
N[]=;
for (llg i=;i<=maxn-;i++) N[i]=N[i-]*i,N[i]%=md;
} llg ksm(llg a,llg b,llg c)
{
if (b==) return ;
a%=md;
llg ans=;
while (b!=)
{
if (b%) ans*=a,ans%=md;
b/=;
a*=a, a%=md;
}
return ans;
} int main()
{
cin>>T;
N[]=D[]=;
maken(),make_D();
while (T--)
{
scanf("%lld%lld",&n,&m);
llg x=(N[n-m]*N[m]) % md;
llg ni=ksm(x,md-,md);
printf("%lld\n",((N[n]*ni) % md)*D[n-m] % md);
}
return ;
}
BZOJ 4517: [Sdoi2016]排列计数的更多相关文章
- 数学(错排):BZOJ 4517: [Sdoi2016]排列计数
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 693 Solved: 434[Submit][Status ...
- BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]
4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...
- BZOJ 4517: [Sdoi2016]排列计数 错排公式
4517: [Sdoi2016]排列计数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4517 Description 求有多少种长度为 ...
- BZOJ 4517: [Sdoi2016]排列计数 错排+逆元
4517: [Sdoi2016]排列计数 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i, ...
- Bzoj 4517: [Sdoi2016]排列计数(排列组合)
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MB Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ...
- BZOJ.4517.[SDOI2016]排列计数(错位排列 逆元)
题目链接 错位排列\(D_n=(n-1)*(D_{n-1}+D_{n-2})\),表示\(n\)个数都不在其下标位置上的排列数. 那么题目要求的就是\(C_n^m*D_{n-m}\). 阶乘分母部分的 ...
- BZOJ 4517: [Sdoi2016]排列计数(组合数学)
题面 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m ...
- bzoj 4517: [Sdoi2016]排列计数【容斥原理+组合数学】
第一个一眼就A的容斥题! 这个显然是容斥的经典问题------错排,首先考虑没有固定的情况,设\( D_n \)为\( n \)个数字的错排方案数. \[ D_n=n!-\sum_{t=1}^{n}( ...
- BZOJ 4517: [Sdoi2016]排列计数 错排 + 组合
从 $n$ 个数中选 $m$ 个不错排,那就是说 $n-m$ 个数是错排的. 用组合数乘一下就好了. Code: #include <cstdio> #include <algori ...
随机推荐
- Error : An error occurred while creating the WebJob schedule: Response status code does not indicate success: 409 (Conflict).
How to fix the error? the answer is simple switch from Free to Standard...
- 关于nodejs能同时接受多少个请求的问题?////zzz
关于nodejs能同时接受多少个请求的问题? 最近学习node,看了很多教程,都在赞扬nodejs的异步I/O,异步I/O的特点就是,每接收一个请求,使用异步调用处理请求,不用等待结果,可以继续运行其 ...
- Windows Phone 六、JSON序列化
JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...
- dataset转list实体
private static object GetDefaultValue(object obj, Type type) { if (obj == DBNull.Value) { return def ...
- 优化 bulk insert
https://www.simple-talk.com/sql/learn-sql-server/bulk-inserts-via-tsql-in-sql-server/
- 25个实用的jQuery技巧和解决方案
1. 去除页面的右键菜单 $(document).ready(function(){ $(document).bind(“contextmenu”,function(e){returnfalse;}) ...
- gerrit add review标签
添加verifyed标签 http://blog.csdn.net/terence427/article/details/16840697
- 2 . Linux常见命令
Linux常见命令格式: 命令名称 选项 参数 ls -alh 文件/目录 --all #ls 显示当前目录内容 #ls -l 显示文件详细信息 #ls -hl h已常见单位显示文件大小 k M G# ...
- Mysql查询重复记录
第一步 使用group by 和 having cout 查找重复字段 SELECT t1.`order_book_id` FROM `quant_stock_info` t1 GROUP BY t1 ...
- 实时发布到tomcat
当我们在运用eclipse进行web项目的文件编写的时候,希望编写的东西可以实时的发布在tomcat的webapps文件夹之中,那么我们应该怎么做呢!下面就是操作方法: 1. 首先你的eclips ...