UPC- access arrangement (Inverse Cantor expandable)

Learning is like rowing upstream

UPC- access arrangement (Inverse Cantor expandable)

Cantor Expand ← I point my point

Take 1 to N were N consecutive numbers (1≤N≤9), consisting of all possible N bits do not overlap each number, in ascending order numbered. When a number input M, N the number of bits that can be printed out to the corresponding number. For example, when N = 3, can be composed of all three-digit: Here Insert Picture DescriptionSo, when the input number M = 2, the output 132.

Entry

It comprises two numbers, i.e., a positive integer N (1 ≤ N ≤ 9) and the positive integer M (1 ≤ M ≤ 362880).

Export

Only one line, i.e., the number N and M bits corresponding to the input.

Sample Input

3 2

Sample Output

132

This question of when to take the road, I suddenly thought of two months ago suddenly saw an algorithm called the
Cantor Cantor algorithm.Full array Cantor algorithm, which means that a sequence made the following descending order, then the algorithm obtained by Cantor This is the first of severalFor example, the original sequence is then 123 123 for the first term to the second 132, and so forth. . .
Then you will find this place and meaning of the questions are exactly the opposite, OK then we Cantor algorithm based on inverse operation. First, do some positive operation.

Cantor Expand

Cantor expansion follows
Described as a bunch of words
First, to add 1, No. 1 since the sequence from the beginning.
You define a number of columns such as 12345, a given disorder.
53412, how this is the first of several.
We define N as the current status of all digital numbers, is he still behind the number is smaller than the number of their own, such as the back, then there is no number 1, so is 0.Note that once removed a
5 4 behind it so N . 1 =. 4, so that there are 1234 prosequence.
1 and 3 and there are N 2 and it has two 2 = 2, so that left 124. Such N . 3 = 2, N . 4 = 0, N . 5 = 0.
So after using the expansion
a n s = i = 1 n N i ( n i ) ! + 1 ans=\sum_{i=1}^{n}N_{i}*(n-i)!+1
4 4 ! + 2 3 ! + 2 2 ! + 0 1 ! + 0 0 ! + 1 = 113 4*4!+2*3!+2*2!+0*1!+0*0!+1=113
available ans is 113
Here Insert Picture Description
then
inverse Cantor ExpandHow to playIt?
To play against the chant.
In addition to the factorial is to be fresh to be lost
because it is the first plus 1 so the inverse of the time affirmative minus 1 first
then
113-1 = 112.
OK start by a dream cause.
Also put five series
sequence12345
First with 112 ÷ 4! = 4 ... 16
is small than they are in the entire sequence number is 4 then the number of 4 + 1
is the number 5 is the fifth
sequence into1234
And then with 16 ÷ 3! = 2 ... 4
so that the entire sequence of inside smaller than they have two so this number is the number of 2 + 1
is the third number is 3
sequence becomes124
And then with 4 ÷ 2! = 2 ... 0
so that the whole sequence where there are smaller than their two then the second number is the number of + 1
is the third number is 4
sequence becomes12
And then with a 0 ÷ 1! = 0 ... 0
so that the whole sequence where there are less than their number is 0 then the number 0 + 1
is the number 1 is the first
sequence becomes2
So finally becomes 53412

So there is this problem within the taste! ! !

Time to AC

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include <stack>
#include<string>
#include<math.h>
#include<stdio.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const ll ll_inf=9223372036854775807;
const int int_inf=2147483647;
const short short_inf=32767;
const char char_inf=127;
inline ll read() {
	ll c=getchar(),Nig=1,x=0;
	while(!isdigit(c)&&c!='-')c=getchar();
	if(c=='-')Nig=-1,c=getchar();
	while(isdigit(c))x=((x<<1)+(x<<3))+(c^'0'),c=getchar();
	return Nig*x;
}
#define read read()
ll j[]= {0,1,2,6,24,120,720,5040,40320,362880,3628800};
ll save[]= {1,2,3,4,5,6,7,8,9};
int main() {
	ll n,m;
	cin>>n>>m;
	ll t=m-1;
	for(ll i=n-1; i>=1; i--) {
		ll temp=t/j[i]+1;
		for(ll j=0; j<n; j++) {
			if(save[j]==-1)continue;
			temp--;
			if(!temp) {
				printf("%lld",save[j]);
				save[j]=-1;
				break;
			}
		}
		t%=j[i];
	}
	for(int i=0; i<n; i++)
		if(save[i]!=-1) printf("%lld",save[i]);
	return 0;
}

Front store the following factorial content so it would be more convenient.
Hou Kangtuo formula to understand better the ratio of consumption dfs

By- wheel month

There is a road ground for the tracks, Boundless Learning bitter for the boat.
Published 32 original articles · won praise 12 · views 1189

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104270075