HDU - 3664 Permutation Counting
Input
There are several test cases, and one line for each case, which contains two integers, N and k. (1 <= N <= 1000, 0 <= k <= N).
Output
Output one line for each case. For the answer may be quite huge, you need to output the answer module 1,000,000,007.
Sample Input
3 0
3 1
Sample Output
1
4
Hint
There is only one permutation with E-value 0: {1,2,3}, and there are four permutations with E-value 1: {1,3,2}, {2,1,3}, {3,1,2}, {3,2,1} 套路题。
先用dp求出f[i] 为至少有i对满足关系的排列数,然后再二项式反演一下就好啦。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=1000;
int jc[maxn+5],ni[maxn+5],n,k,f[maxn+5];
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an;}
inline int getC(int x,int y){ return x<y?0:jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;} inline void init(){
jc[0]=1;
for(int i=1;i<=maxn;i++) jc[i]=jc[i-1]*(ll)i%ha;
ni[maxn]=ksm(jc[maxn],ha-2);
for(int i=maxn;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
} inline void solve(){
memset(f,0,sizeof(f)); f[0]=1;
for(int i=n;i;i--)
for(int j=n-i;j>=0;j--) f[j+1]=add(f[j+1],f[j]*(ll)(n-i-j)%ha);
for(int i=0;i<=n;i++) f[i]=f[i]*(ll)jc[n-i]%ha; int ans=0;
for(int i=k;i<=n;i++)
if((i-k)&1) ans=add(ans,ha-getC(i,k)*(ll)f[i]%ha);
else ans=add(ans,getC(i,k)*(ll)f[i]%ha);
printf("%d\n",ans);
} int main(){
init();
while(scanf("%d%d",&n,&k)==2) solve();
return 0;
}
HDU - 3664 Permutation Counting的更多相关文章
- hdu 3664 Permutation Counting(水DP)
Permutation Counting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU - 3664 Permutation Counting 排列规律dp
Permutation Counting Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the ...
- HDU 3664 Permutation Counting (DP)
题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...
- HDU 6880 Permutation Counting dp
题意: 给你一个n和一个长度为n-1的由0/1构成的b序列 你需要从[1,n]中构造出来一个满足b序列的序列 我们设使用[1,n]构成的序列为a,那么如果ai>ai+1,那么bi=1,否则bi= ...
- HDU3664 Permutation Counting
Permutation Counting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)
题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...
- hdu3664 Permutation Counting(dp)
hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
随机推荐
- JAVA基础篇—HashMap
/class Depositor package 银行储户; public class Depositor { private String id; private String name; priv ...
- 【南邮】md5 collision write up
源码: <?php $md51 = md5('QNKCDZO'); $a = @$_GET['a']; $md52 = @md5($a); if(isset($a)){ if ($a != 'Q ...
- 数学基础:HUD1124-Factorial(N!末尾0的个数)
Factorial Problem Description The most important part of a GSM network is so called Base Transceiver ...
- UVa 1366 DP Martian Mining
网上的题解几乎都是一样的: d(i, j, 0)表示前i行前j列,第(i, j)个格子向左运输能得到的最大值. d(i, j, 1)是第(i, j)个格子向上运输能得到的最大值. 但是有一个很关键的问 ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- body标签相关
03-body标签中相关标签 今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一 ...
- [每日app二]月入60万多嘛?单词锁屏的潜力!
抢了用户的时间,就是抢了用户的金钱! 单词锁屏,一个开发难度不太大,但仅仅360手机助手下载就是每周4万!拉风- 对于搞app的同学来说,搞个锁屏,还不是玩似的,但是要定位好,玩得好,那就有难度了.最 ...
- 使用MySQLWorkBench连接数据库
在日常使用中,经常使用命令行去连接MySQL数据库不方便,推荐使用MySQLWorkBench去远程连接进行管理,下面记录一下使用步骤: 1. 安装MySQLWorkBench后,界面如下,点击“+” ...
- JDBC 学习笔记(五)—— Statement
JDBC 使用 Statement 作为 SQL 语句的执行器. Statement 通过 Connection.createStatement() 方法创建,一共支持以下6种方式执行 SQL 语句: ...
- hihoCoder #1117 战争年代
题目大意 对一棵树的节点染色.初始时每个点都染成颜色 $0$,然后进行 $m$ 轮操作.第 $i$ 轮操作:从 $[0,d_i]$ 中随机选出一个整数 $d$,将距离点 $x_i$ 不超过 $d$ 的 ...