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. testNg-build.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <proje ...

  2. centos 升级内核(编译安装)

    yum install -y wget gcc gc bc gd make perl ncurses-devel xz下载地址:https://www.kernel.org#tar -Jxvf lin ...

  3. robotium学习及整理

    一.                      Robotium 简介 Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手 ...

  4. C# 源码计数器

    设计背景 编程工作中,有些文档需要填写代码量,例如申请软件著作权.查阅相关资料之后,编写了这个小程序. 设计思路 主要思路为分析项目文件,根据项目文件查找代码文件,然后遍历代码文件进行分析 相关技术 ...

  5. python beautifulsoup/xpath/re详解

    自己在看python处理数据的方法,发现一篇介绍比较详细的文章 转自:http://blog.csdn.net/lingojames/article/details/72835972 20170531 ...

  6. [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...

  7. C++ STL 常用排序算法

    C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...

  8. PowerDesigner在生成SQL时报错Generation aborted due to errors detected during the verification of the mod

    一.本章节要用到  ODBC连接数据库直接创建表,请先创建连接库的ODBC 请参考  新建  http://www.cnblogs.com/wdw31210/p/7580286.html 二.生成 去 ...

  9. IoT与区块链的机遇与挑战

    区块链, 分布式账本技术的一种形式, 自从2014年或多或少地获得了大量的关注: 区块链和物联网, 区块链和安全, 区块链和金融, 区块链和物流, 凡是你能想到的,仿佛都可以应用区块链. 在本文中, ...

  10. Linux内核设计与实现第八周读书笔记

    第四章 进程调度 进程在操作系统看来是程序的运行态表现形式. 4.1多任务 多任务操作系统就是能同时并发地交互执行多个进程的操作系统. 多任务操作系统会使多个进程处于堵塞或者睡眠状态.这些任务尽管位于 ...