Cantor expand self notes

The original title on board in Los Valley
https://www.luogu.org/problem/P2524
Of course, I do it a little bit. . . Fenwick tree used
mainly formula
A [. 1] (. 1-n-) + ....... + A [I] (Ni)! + A [I +. 1] (-Ni. 1)! ... a .... [n-] 0!
(Ni)! factorial is, a [i] is the i in this position, there is a [i] is smaller than the number of his (a [n] is of course 0)
revision reverse correct!
I understand this formula is: when i have this position a [i] is smaller than the number of it, then it has (ni) may be arranged, are arranged before that (of course push is purely my sentence! ) anyway, it sets the formula
below is loaded to force version of the template

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map> 
using namespace std;
#define re register int
inline int read(){
    int x=0,ff=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')ff=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return x*ff;
}
int n,c[15],f[15],ss;
inline int fs(int x){return x&-x;}
struct tt{
    int ii,x;
}b[15];
char a[15];
bool cmp(tt xx,tt yy){
    return xx.x<yy.x;
}
inline int find(int x){
    int s=0;
    while(x<=n){
        s+=f[x];x+=fs(x);
    }
    return s;
}
inline void add(int x){
    while(x){
        f[x]++;x-=fs(x);
    }
}
signed main(){
    n=read();c[1]=1;
    for(int i=2;i<=n;i++)c[i]=c[i-1]*i;
    scanf("%s",a+1);
    for(int i=1;i<=n;i++){
        b[i].x=a[i]-'0';b[i].ii=i;
    }
    sort(b+1,b+n+1,cmp);
    for(int i=1;i<=n;i++){
        ss+=find(b[i].ii)*c[n-b[i].ii];
        add(b[i].ii);
    }
    printf("%d\n",ss+1);
    return 0;
}

Of course, there is an inverse Cantor expansion
, for example, give you a permutation of n elements, to tell you it was the first (x + 1) th arrayed (ie, Cantor launched calculated values, health groups launched the first arrangement worked out is 0, moves back up a whole, it can be understood starting from 0) (above code 1 output is added)
then we can use to modulo factorial
first with x% ((n + 1) !) , assuming calculated is y, then illustrate the y small in its elements behind it, then it is the remaining section (y + 1) element (sorted by value)
and then let x = x / ((n + 1) !), delete the current number of selected, sorted
and then do let down% the X-(the n-!)
................
until 1 factorial!, the rest is a number Finally, a number of
a cited example:
given 61 (1,2,3,4,5) can be calculated from the permutations and combinations to 34152
in the following steps:
! with 61/4 2 I = 13, description, description smaller than the first there are a number of two, the first three.
With 13/3! = 1 2 it, instructions described after the second number is smaller than the second has 2, the second 4 bits.
1/2! = I 01, instructions described after the third bit is not less than the third number, the third bit is 1.
1/1! = I 1 0, instructions described after the second number is less than the fourth bit is 1, the fourth bit 5.
Examples In:
---------------------
Author: i-Curve
Source: CSDN
Original:https://blog.csdn.net/qq_38701476/article/details/81003290
thanks example of big brother.
It should Cantor launched on these, and on the set formula to test it

Guess you like

Origin www.cnblogs.com/ffrxy01bt/p/11260951.html