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 ...
随机推荐
- Leetcode: Circular Array Loop
You are given an array of positive and negative integers. If a number n at an index is positive, the ...
- 0-js判断电脑或手机登录
<script type="text/javascript"> try{ if(/Android|webOS|iPhone|iPod|BlackBerry/i.test ...
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- People Tools catalog tables.
People Tools catalog tables. Projects PSPROJECTDEFN — Project header table PSPROJECTITEM — Definitio ...
- 初次了解的Java多线程
0.1熟悉多线程 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能 ...
- 代码高亮美化插件-----SyntaxHighlighter
IT类文章博客,代码高亮美化插件-----SyntaxHighlighter 最近在做一个类似个人博客的网站,因为文章中会用到各种代码,主要是Javascript,CSS,PHP,XML等.这些代码如 ...
- 【实践】js实现简易的四则运算计算器
最近看了一个大神推荐的某公司面试程序员的js 面试题,题目是用js 做一个计算器于是跟着大神的思想自己做了一下 ps:功能还没有完善好毕竟自己还是一只菜鸟还在不断学习中. 闲话不多说先上css代码 & ...
- Terra Vista 6.2
最近在做虚拟仿真相关工作,想把GIS中的一些想法用虚拟显示技术实现,在保证准确性的同时,提高展现效果. 前不久在朋友圈获得了一个强大的三维地形构建软件Terra Vista 6.2,据说这个软件是加拿 ...
- 乐1S 5.8(Android 6.0) 刷第三方recovery并刷入root权限
说明 我的是 乐视1S, 系统为EUI 5.8 (powered by Android 6.0) 使用的是 superSu来root, 你可以到这里下载 文件 *下载 Recovery Flashab ...
- JS IOS/iPhone的Safari不兼容Javascript中的Date()问题
var date = new Date('2016-11-11 11:11:11'); document.write(date); 最近在写一个时间判断脚本,需要将固定好的字符串时间转换为时间戳进行比 ...