[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 ...
随机推荐
- java 自定义异常的回顾
一.异常的分类: 1.编译时异常:编译时被检测的异常 (throw后,方法有能力处理就try-catch处理,没能力处理就必须throws).编译不通过,检查语法(其实就是throw和throws的配 ...
- js中的php rand函数
//文件rand.js function MyRand(min, max){ this.min = min; this.max = max; } MyRand.prototype.getRand = ...
- Android ComponentName的用法
ComponentName(组件名称)是用来打开其他应用程序中的Activity或服务的. 用法: Intent it=new Intent(); it.setComponent(new Compon ...
- ie8 ajaxSubmit 上传文件提示下载
转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json,返回text/html
- LeetCode 717. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented by one bit 0. The second char ...
- Avito Cool Challenge 2018 自闭记
A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...
- BZOJ5118 Fib数列2(矩阵快速幂)
特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- poj 2185
http://poj.org/problem?id=2185 题意:求最小的模式块,使其无限扩展后包含给你的矩阵块(看别人题解才懂的题意): 分析:假设存在一个模式块可以满足上述条件,那么必然存在一个 ...
- Java 信号量 Semaphore 介绍
Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有应用.Java 并发库 的Semaphore 可以很轻松完成信号量控制,Semaphore可以 ...
- 创建一个背景透明的UIViewController
有时候想让UIViewController背景透明,让我们可以看到下层的UI,直接设置它的背景颜色为clearColor(),还是有黑色的默认背景在那里.下面是解决该问题的例子: 在storyboar ...