[gym100956]Problem J. Sort It! BIT+组合数
source : Pertozavodsk Winter Training Camp 2016 Day 1: SPb SU and SPb AU Contest, Friday, January 29, 2016
url:https://codeforces.com/gym/100956/attachments
-----------------------------------------------------
题意:
有一个1~n的全排列p1~pn,问有多少个长度为n的数组,满足
1.数组中每个元素均为1~n的正整数
2.按照全排列的顺序,i从1到n,依次将数组中等于pi的元素拿出来放在新数组末端,完成后新数组为有序的。
-----------------------------------------------------
题解:
样例
3
2 1 3
含1个不同元素的数组:
1:1个
2:1个
3:1个
含2个不同元素的数组:
2,3:2^3-1个
1,3:2^3-1个
解法:
求出原排列中长度为1~n的上升子序列有多少个,记为len[i];
求出严格含1~n个不同元素的n位的数组有多少种,记为f[i];
则ans = sigma(len[i]*f[i])
求len[i]:递推,已知以第j位为结尾的长度为x的上升子序列有sum_prelen[j]个,则以第i位为结尾长度为x+1的上升子序列数量=sigma(sum_prelen[1~i-1])。用树状数组维护。
求f[i]:f[i]=i! - sigma(f[1~i-1])
-------------------------------------------------
代码如下:
#include<bits/stdc++.h>
using namespace std; typedef long long LL;
const int N=;
const LL mod=(LL)1e9+;
int n;
LL c[N],sum_prelen[N],len[N],val[N],jc[N],f[N]; void readin(LL &x)
{
x=;bool f=;char ch=getchar();
while(!isdigit(ch)) {
f|=(ch=='-');
ch=getchar();
}
while(isdigit(ch)) {
x=(x<<)+(x<<)+ch-;
ch=getchar();
}
if(f) x=-x;
} void add(LL x,LL d){
for(int i=x;i<=n;i+=(i&(-i))) c[i]=(c[i]+d)%mod;
}
LL getsum(LL x){
LL ans=;
for(int i=x;i>=;i-=(i&(-i))) ans=(ans+c[i])%mod;
return ans;
} LL mypow(LL x,LL y){
LL ans=;
while(y)
{
if(y&) ans=ans*x%mod;
x=x*x%mod;
y>>=;
}
return ans;
} LL mod_inverse(LL x,LL n){
return mypow(x,n-);
} LL cal_C(LL x,LL y){
// C(x,y)=y!/(x!(y-x)!)
return jc[y] * mod_inverse(jc[x],mod) % mod * mod_inverse(jc[y-x],mod) % mod;
} int main()
{
freopen("a.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++) readin(val[i]);
for(int i=;i<=n;i++) sum_prelen[i]=;
len[]=n;
for(int i=;i<=n;i++)
{
len[i]=;
for(int j=;j<=n;j++) c[j]=;
for(int j=;j<=n;j++)
{
LL sum_nowlen=getsum(val[j]-);
len[i]=(len[i]+sum_nowlen)%mod;
add(val[j],sum_prelen[j]);
sum_prelen[j]=sum_nowlen;
}
// for(int j=1;j<=n;j++) printf("%lld ",len[j]);printf("\n");
} jc[]=;for(int i=;i<=n;i++) jc[i]=(jc[i-]*((LL)i))%mod;
f[]=;
LL ans=;
for(int i=;i<=n;i++)
{
f[i]=mypow(i,n);
for(int j=;j<i;j++)
f[i]=((f[i]-cal_C(j,i)*f[j]%mod)%mod+mod)%mod;
ans=(ans+len[i]*f[i]%mod)%mod;
} printf("%I64d\n",ans);
return ;
}
[gym100956]Problem J. Sort It! BIT+组合数的更多相关文章
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- 实验12:Problem J: 动物爱好者
#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Problem J: 求个最大值
Problem J: 求个最大值 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 871 Solved: 663[Submit][Status][Web ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem J. Joke 水题
Problem J. Joke 题目连接: http://codeforces.com/gym/100714 Description The problem is to cut the largest ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
随机推荐
- json 数组
/** * 获取 选中的id * @returns {*} */function getCheckedList(){ var groups =[]; $("input[name='check ...
- Internet History, Technology and Security (Week 5-1)
Week 5 Technology: Internets and Packets Welcome to Week 5! This week, we'll be covering internets a ...
- Alpha阶段敏捷冲刺⑥
1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 实现对账单条目的编辑 初步设计设置页面 今天要完成的工作. 账单明细 ...
- how install svn client on MacOS
how install svn client on MacOS svn https://www.smartsvn.com/downloads/smartsvn/smartsvn-macosx-11_0 ...
- Windows安装ElastAlert问题总结
1.运行时出现UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xb4 in position 0: invalid start byte 或 ...
- 在tensorflow环境下安装matplotlib
在运行程序时,报错ImportError: No module named 'matplotlib',如图.经网上查询发现是没有安装matplotlib 因此记录一下在tensorflow环境下安装m ...
- zabbix 自定义监控nginx
zabbix自定义nginx监控项 查看nginx编译安装是否加上该选项,如果没有请重新编译安装 配置nginx.conf vim /usr/local/cpgroup/nginx/conf/vhos ...
- ButterKnife注入注解框架用法
Android 依赖注入 ButterKnife 基本使用 - 渐行渐远渐无声 - 博客园http://www.cnblogs.com/fansen/p/5653887.html ButterKnif ...
- servlet的application对象的使用
application对象 1 什么是application对象 ? (1) 当Web服务器启动时,Web服务器会自动创建一个application对象.application对象一旦创建,它将一直存 ...
- HDU 3271 数位dp+二分
SNIBB Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...