题意略。

思路:

很经典的逆序对计数问题。

https://blog.csdn.net/v5zsq/article/details/79006684

这篇博客讲得很好。

当循环到n的时候,我们需要特殊考虑,因为在循环内,它的贡献为0,所以我们在出循环的时候,还要特殊地加上一个值,也即整体的逆序对个数。

详见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
const int maxn = 1e6 + ; int p[maxn],n;
LL fac[maxn],f[maxn],BIT[maxn]; void init(){
fac[] = fac[] = ;
for(LL i = ;i < maxn;++i){
fac[i] = i * fac[i - ] % mod;
LL C = i * (i - ) / % mod;
f[i] = C * C % mod * fac[i - ] % mod;
}
}
int lowbit(int k){
return k & -k;
}
void add(int pos,LL val){
while(pos <= n){
BIT[pos] += val;
pos += lowbit(pos);
}
}
LL sum(int pos){
LL ret = ;
while(pos > ){
ret += BIT[pos];
pos -= lowbit(pos);
}
return ret;
} int main(){
init();
while(scanf("%d",&n) == ){
memset(BIT,,sizeof(BIT));
for(int i = ;i <= n;++i) scanf("%d",&p[i]);
LL ans = ,s = ;
for(int i = ;i <= n;++i){
LL useful = p[i] - - sum(p[i]);
ans = (ans + s * useful % mod * fac[n - i] % mod) % mod;
ans = (ans + useful * f[n - i] % mod) % mod;
ans = (ans + useful * (useful - ) / % mod * fac[n - i] % mod) % mod;
s += useful;
s %= mod;
add(p[i],);
}
ans = (ans + s) % mod;
printf("%lld\n",ans);
}
return ;
}

Codeforces 396D的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. 利用DOMNodeInserted监听标签内容变化

    var exeFlag = 0;//控制执行业务次数标记$('#list1').bind('DOMNodeInserted', function () { if(!/img/.test($(" ...

  2. 嵌套&匿名&高阶函数

    嵌套&匿名&高阶函数 嵌套函数 函数可以嵌套定义并调用函数 name = "小明" def change(): name = "小明,你好" d ...

  3. Pinyin4j简单使用教程

    Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音输出格式可以定制,在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,这时候Pinyin4j就可以派上用场 有自己私服的可以 ...

  4. 逆向MFC程序

    目录 @ 1 MFC执行流程 1.1 环境支持 1.2 分析 1.3 实践探索 1.3.1 创建一个MFC程序 1.3.2 下关键断点并调试 1.4 转向MFC库源文件中观测 2 逆向 2.1 特征码 ...

  5. Linu基础之权限管理

    二十二.权限管理 22.1)什么是权限 针对某些文件或者进程,对用户进行限制,权限可以理解为用于约束用户能对系统所做的操作. 22.2)权限和用户的关系   [root@centos7 ~]# ll ...

  6. 《HTML总结》

    一.HTML简介 Hyper Text Markup Language(超文本标记语言) 超文本包括:文字.图片.音频.视频.动画等 二.HTML发展史 1993-6发布超文本标记语言 ....... ...

  7. About dycf

    SYSU  数媒在读 所有资料可能与课程相关可能与参与项目相关 欢迎交流 . . . 中之人: ↓↓↓ ↑↑↑   是他  就是他  ↑↑↑

  8. 【译】Hello Kubernetes快速交互实验手册

    原文:https://kubernetes.io/docs/tutorials 翻译:Edison Zhou 一.基本介绍 此交互实验可以让你不用搭建K8S环境就可以轻松地尝试管理一个简单的容器化应用 ...

  9. centos6.5-7编译安装Ansible详细部署

    一.基础介绍==========================================================================================ansi ...

  10. java课堂 动手动脑3

    (1) 该函数没有赋初值再就是如果类提供一个自定义的构造方法,将导致系统不在提供默认的构造方法. (2) public class test { public static void main(Str ...