好久没写题解了。。GDKOI被数位DP教做人了一发,现在终于来填数位DP的大坑了>_<。

  发现自己以前写的关于数位DP的东西...因为没结合图形+语文水平拙计现在已经完全看不懂了嗯。

  看来看去感觉还是这篇关于数位DP的介绍靠谱:http://wenku.baidu.com/view/d2414ffe04a1b0717fd5dda8.html

  想题的时候结合图形食用效果更佳。

  题意是说,对于一个给定的n的排列,要求出 (所有字典序<给定排列的排列)的逆序对个数和。

  预处理出f[i]表示i的所有排列中,逆序对的个数。(事实上也是i个数的所有排列中逆序对的个数)

  然后像那篇论文里面的姿势处理就好了。

  每次把逆序对分为三种:已确定的数和后面的数之间的逆序对、已确定的数之间的逆序对,后面的数之间的逆序对。

  前两种可以放到一起统计(和后面的数的具体顺序无关),第三种就是预处理出来的东西。

  顺便在hdu上压了发代码最短= =(空间实在无力>_<

 #include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll modd=;
ll f[],a[];//a数组存阶乘的值
ll i,j;
int n,x;
bool u[];
inline ll run(){
ll ans=,now=,mn;
memset(u,,n+);
for(i=;i<=n;i++){
scanf("%d",&x);u[x]=;
for(j=,mn=;j<x;j++)
if(!u[j])ans+=f[n-i]+a[n-i]*(now+mn),ans%=modd,mn++;
for(j=;j<x;j++)if(!u[j])now++;
}
return ans;
}
int main(){
a[]=;f[]=;
for(i=;i<=;i++){
a[i]=a[i-]*i%modd;
f[i]=(i*f[i-]+a[i-]*(i-)*i/)%modd;
}
while(scanf("%d",&n)==)printf("%lld\n",run());
return ;
}

[hdu5225][BC#40]Tom and permutation的更多相关文章

  1. hdu 5225 Tom and permutation(回溯)

    题目链接:hdu 5225 Tom and permutation #include <cstdio> #include <cstring> #include <algo ...

  2. BestCoder Round #40

    T1:Tom and pape (hdu 5224) 题目大意: 给出一个矩形面积N,求周长的最小值.(长&&宽&&面积都是正整数) N<=109 题解: 没啥好 ...

  3. URL编码 URLEncoder 示例

    2016-12-27 对字符编码时的规则 通常如果一样东西需要编码,说明这样东西并不适合传输.原因多种多样,如Size过大,包含隐私数据. 对于Url来说,之所以要进行编码,一个是因为Url中有些字符 ...

  4. xrdp的rdp前端无法连接Windows 2008的问题

    xrdp使用rdp前端,无法连接2008,但连接2003是可以的.连接2008的时候,会在客户端发送Client Info PDU后主动RST掉连接.如下图 开始以为是客户端发送Client Info ...

  5. j详细说明ava于clone办法

    原文地址:http://leihuang.org/2014/11/14/java-clone/ In java, it essentially means the ability to create ...

  6. GO 语言简介(网摘)

    GO 语言简介 原文出处:[陈皓 coolshell] Hello World 文件名 HELLO.GO package main //声明本文件的package名 import "fmt& ...

  7. spark-shell的Scala的一些方法详解

    Tom,DataBase,80 Tom,Algorithm,50 Tom,DataStructure,60 Jim,DataBase,90 Jim,Algorithm,60 Jim,DataStruc ...

  8. Go 语言简介(上)— 语法

    周末天气不好,只能宅在家里,于是就顺便看了一下Go语言,觉得比较有意思,所以写篇文章介绍一下.我想写一篇你可以在乘坐地铁或公交车上下班时就可以初步了解一门语言的文章.所以,下面的文章主要是以代码和注释 ...

  9. Go语言入门系列2 基本语法

    get download and install packages and dependencies install = compile and install packages and depend ...

随机推荐

  1. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  2. 什么是副作用(Side Effect)

    副作用(Side Effect)是指函数或者表达式的行为依赖于外部世界.具体可参照Wiki上的定义,副作用是指 1)函数或者表达式修改了它的SCOPE之外的状态 2)函数或者表达式除了返回语句外还与外 ...

  3. adb指令介绍

    一.adb命令格式为:adb [-d|-e|-s <serialNumber>] <command> 1.adb devices :列出当前电脑所连接的所有安卓设备 2.adb ...

  4. Search an Element in an array

    Given an integer array and an element x, find if element is present in array or not. If element is p ...

  5. JS画几何图形之六【过直线外一点作垂线】

    样例:http://www.zhaojz.com.cn/demo/draw10.html 依赖:[点].[直线] //过直线外一点画垂线 function drawVerticalLine(point ...

  6. Lucene分词停用词库stopwords

    ! " $ % & ' ( ) * + , - -- . .. ... ...... ................... ./ .一 .数 .日 / // 0 1 2 3 4 5 ...

  7. Talk 3: Rob Pike on Upspin (Gopherfest 2017)

    Talk 3: Rob Pike on Upspin Upspin is an experimental project to build a framework for naming and sha ...

  8. js构建函数优秀案例

    这几个效果函数是看到别人写的,挺好的,复制下来学习备用! 函数封装: //var _hmt = _hmt || [];(function() {var hm = document.createElem ...

  9. 通过js添加的元素点击事件无法触发

    var blk_have ='<div class="sw-off"></div>'; $('#blk').prepend(blk_have); $(doc ...

  10. eval基础,基础用法及解析json

    <body> <!-- eval 的使用:eval(string) 计算某个字符串,并执行其中的js代码 字符串上运用 eval() eval("x = 10;y = 2; ...