LuoguP1088 Martian

EDITORIAL

\ (STL \) is good, but can not remember who (like me this cb), to use the other solution

Hexadecimal number

Idea

link

How do we think about it?

For the first \ (I \) finger, there \ (n-i + 1 \ ) choices, then the median is the \ (n-i + 1 \ ) hex

Our three-step process

  1. Mars will become hexadecimal number
  2. Binary number \ (+ m \)
  3. The hexadecimal number in the back of Mars

We give chestnuts: Sample

The 1,2,3,4,5 becomes binary number

  • The first five choices 1 \ (\ {1,2,3,4,5 \} \) of the first type, it becomes 0 (zero)
  • Bit times four choices 2 \ (\ {2,3,4,5 \} \) of the first type, it becomes 0
  • 3 is meditope three options \ (\ {3,4,5 \} \) of the first type, it becomes 0
  • 4 is two times lower choices \ (\ {4,5 \} \) of the first type, it becomes 0
  • 5 is the last bit one kind selected \ (\ {5 \} \) the first type, it becomes 0
  • Finally, 1,2,3,4,5 becomes \ ((00000) _ {unknown } \)

Wherein for each bit is \ (n-i + 1 \ ) binary number.

Thus, \ ((00000) _ {+}. 3 = Unknown (00110) Unknown} _ {\)

001.jpg

(Write ugly, do not care)

Reconversion back

  • 0 represents the first choice to be $ 4 $ {1,2,3,4,5} the first type, i.e., 1
  • Bit 0 indicates the times should be selected \ (\ {2,3,4,5 \} \) the first type (1 selected over), i.e., 2
  • Bit 1 indicates the intermediate should be selected \ (\ {3,4,5 \} \) a second species, i.e. 4
  • 1 represents the low times should be selected \ (\ {3,5 \} \) the second type, i.e. 5
  • The selection should be the last one represents 0 \ (\ {3 \} \) the first type, i.e., 3
  • So the answer to this question is "14523" + 3 = "14253"

So, the code presented. Including next_permutationthe wording

Code

int a[maxn];
bool vis[maxn];
int n,m;
int main(){
    n=read(); m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        int x=a[i];
        for(int j=1;j<=a[i];j++) x-=vis[j];
        vis[a[i]]=1;
        a[i]=x-1;
    }
    a[n]+=m;
    for(int i=n;i;i--){
        a[i-1]+=a[i]/(n-i+1);
        a[i]%=n-i+1; 
    }
    mem(vis,0);
    for(int i=1;i<=n;i++){
        for(int j=0;j<=a[i];j++)
        if(vis[j]) a[i]++;
        printf("%d ",a[i]+1);
        vis[a[i]]=1; 
    }
    return 0;
}
int a[maxn];
int main(){
    int n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    while(m--) next_permutation(a+1,a+n+1);
    for(int i=1;i<n;i++) printf("%d ",a[i]);
    printf("%d\n",a[n]);
    
    return 0;
}

\[ The \quad End \]

\ [\ Text {If the boy crying tired smile, said to go wandering; left adult look, look at loggerheads years there will always be your personal distance. - "Wrangler City" Mao difficult} \]

Guess you like

Origin www.cnblogs.com/cbyyc/p/11720227.html