排列逆序对计数

https://pan.baidu.com/s/1mRenOxuGQ9gEpW7WsJXf4Q    7uqg 

组合计数,结合数位DP的思想。

考试话3小时做出来了,结果没开LL,50分,很崩溃

https://www.cnblogs.com/universeplayer/p/10565654.html

 1 #include
 2 using namespace std;
 3 #define rg register
 4 typedef long long ll;
 5 #define gmax(a,b) a=max(a,b)
 6 #define gmin(a,b) a=min(a,b)
 7 #define FOR(i,a,b) for(rg int i=a;i<=b;++i)
 8 #define For(i,a,b) for(rg int i=a;i>=b;--i)
 9 #define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),pa==pb)?EOF:*pa++
10 static char buf[100000],*pa(buf),*pb(buf);
11 inline int rd()
12 {
13     rg int x(0),w(1);
14     rg char c(gc);
15     while(c<'0' || c>'9')
16     {
17         if(c=='-') w=-1;
18         c=gc;
19     }
20     while(c>='0' && c<='9') x=x*10+c-48,c=gc;
21     return x*w;
22 }
23 
24 const int N=1000005;
25 const ll MOD=998244353,ny=499122177;
26 ll f[N],g[N],tn[N],jc[N];
27 int c[N],a[N],n;
28 ll ans;
29 
30 void add(int x,int y)
31 {
32     for(;x<=n;x+=x&(-x)) c[x]+=y;
33 }
34 
35 ll ask(int x)
36 {
37     ll res=0;
38     for(;x;x-=x&(-x)) res+=c[x];
39     return res;
40 }
41 
42 int main()
43 {
44     freopen("sort.in","r",stdin);
45     freopen("sort.out","w",stdout);
46     n=rd();FOR(i,1,n) a[i]=rd();
47     jc[0]=1;FOR(i,1,n) jc[i]=(i*jc[i-1])%MOD;
48     FOR(i,2,n) f[i]=((i*f[i-1])%MOD+(1LL*i*(i-1)/2)%MOD*jc[i-1]%MOD)%MOD;
49     tn[1]=1;add(a[n],1);
50     FOR(i,2,n)
51         g[i]=ask(a[n-i+1]),add(a[n-i+1],1),
52         tn[i]=((g[i]*jc[i-1])%MOD+tn[i-1])%MOD;
53     FOR(i,1,n) ans=(ans+g[i]*f[i-1]%MOD+g[i]*(g[i]-1)%MOD*ny%MOD*jc[i-1]%MOD+g[i]*tn[i-1]%MOD)%MOD;
54     cout<<ans;
55     return 0;
56 }

 

转载于:https://www.cnblogs.com/universeplayer/p/10565737.html

你可能感兴趣的:(排列逆序对计数)