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+组合数的更多相关文章

  1. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  2. 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 ...

  3. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  4. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  5. 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, ...

  6. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  7. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C++模板常用功能讲解

    前言 泛型编程是C++继面向对象编程之后的又一个重点,是为了编写与具体类型无关的代码.而模板是泛型编程的基础.模板简单来理解,可以看作是用宏来实现的,事实上确实有人用宏来实现了模板类似的功能.模板,也 ...

  2. 安装iproute2

    yum install -y gcc bison flex #tar -xzf  iproute2-3.17.0.tar.gz #cd iproute2-3.17.0#sed -i '/^TARGET ...

  3. JVM 规范

    http://files.cnblogs.com/files/dragonsuc/jls8.pdf 或者官网:http://files.cnblogs.com/files/dragonsuc/jls8 ...

  4. python3 执行AES加密方法

    cmd执行命令:pip install pycryptodome # -*- coding: utf-8 -*- # __author__ = 'Carry' import base64 from C ...

  5. ZOJ2760_How Many Shortest Path

    给一个图,求从某个点到另一个点的最短路有多少条?所有的路都不共边. 首先从终点开始Spfa标记最短距离,然后建图. 建图的时候,如果满足两点之间的最短路只差为两点之间的边长,那么在网络流的模型中连接一 ...

  6. python自动化之正则

    import re phoneNumRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') mo=phoneNumRegex.search('My number is ...

  7. GIT的使用方法

    GIT的使用方法 1.电脑首先安装GIT, 2.在官网注册GitHub账号. 一,使用git在控制台进行本地操作 1.打开GitBash 2.填写用户名和邮箱作为标识分别执行以下命令: git/ co ...

  8. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  9. 014 Java的反射机制

    作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...

  10. [提升性选讲] 树形DP进阶:一类非线性的树形DP问题(例题 BZOJ4403 BZOJ3167)

    转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/7337179.html 树形DP是一种在树上进行的DP相对比较难的DP题型.由于状态的定义多种多样,因此解法也五 ...