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: 给你一个字符串,求:至少出 ...
随机推荐
- golang(go语言)调试和查看gc信息,以及gc信息解析
这里记录一下调试golang gc的方法 启用gc打印: # GODEBUG=gctrace=1 go run ./main.go 程序启动后gc将打印如下信息: gc 65 @16.996s 0%: ...
- navicat for mysql 在win7下设置定时计划之导出数据处理
navicat for mysql 在win7下设置定时计划之导出数据处理 博客分类: mysql navitcatmysql定时任务导出 前两篇记录了,navicat for mysql计划的入门 ...
- shell批量修改文件名
[root@localhost file1]# ls a.htm b.htm c.htm d.htm pl.sh [root@localhost file1]# vi pl.sh #!/bin/bas ...
- [POJ 1007] DNA Sorting C++解题
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77786 Accepted: 31201 ...
- 用PHP写的一个简单的分页类 2.0版
<?php /* 分页类 用于实现对多条数据分页显示 version:2.0 //基于1.0 数据库查询用mysqli实现 author:Knight E-Mail:S.Knight.Work@ ...
- struts2对properties资源的处理
struts2对properties资源的处理 做了一些功能增强 包括: 可以读取项的描述 可以读取项所在的行号,文件路径等 实现方式 继承了java的java.util.Properties实现了一 ...
- Leetcode2--->链表中对应位相加(进位)
题目: 给定两个单链表,单链表中的数都是非负数.链表中的数字都是反向存储的,,每个节点都是个位数,将链表对应的位相加,返回最终的结果: 举例: Input: (2 -> 4 -> 3) + ...
- Python学习-day5 常用模块
day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...
- python递归深度报错--RuntimeError: maximum recursion depth exceeded
当你的程序递归的次数超过999次的时候,就会引发RuntimeError: maximum recursion depth exceeded. 解决方法两个: 1.增加系统的递归调用的次数: impo ...
- 【bzoj3251】树上三角形 朴素LCA+暴力
题目描述 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改. 输入 第一行两个整数n.q表示树的点数和操 ...