Expand kangtuo and expand kangtuo

Source: Internet
Author: User

Expand kangtuo and expand kangtuo

1. Explanations of conto


Kanto expansion is a special hash function.

Expand an integer x into the following form:

X = a [n] * n! + A [n-1] * (n-1 )! +... + A [2] * 2! + A [1] * 1!

Where, a is an integer and 0 <= a <I, I = 1, 2,..., n

{1, 2, 3, 4,..., n} indicates the arrangement of 1, 2, 3,..., n, for example, {1, 2, 3}. A total of 6 entries are arranged in ascending order. 123 132 213 231 312 321.

The number 1, 2, 4, 5, and 6 correspond to a given number.

The relationships between them can be found in Kanto.

If you want to know the number of the largest numbers in 321 is {1, 2, 3}, you can consider this as follows:

The first digit is 3. When the first digit is less than 3, the number is smaller than 321, for example, 123 and 213. The number smaller than 3 is 1 and 2. So there are 2*2! . Let's look at the number smaller than the second digit 2: only one number smaller than 2 is 1, so there is 1*1! = 1 so the number of {1, 2, 3} arrays smaller than 321 is 2*2! + 1*1! = 5
. So 321 is a large number of 6th. 2*2! + 1*1! It is Kanto expansion.

For another example, 1324 is the largest number in the {1, 2, 3, 4} number. The first is the number where 1 is smaller than 1. The number is 0 0*3! The second digit is 3 less than 3. The numbers are 1 and 2, but 1 is already in the first place, so there is only one digit 2 1*2! . The third digit is 2 and the number less than 2 is 1, but 1 is in the first place, so

There are 0 numbers 0*1! So there are 0*3 smaller than 1324! + 1*2! + 0*1! = 2, 1324 is the third large number.

(1) identify the number of 96th

First, use 96-1 to get 95

Remove 4 with 95! Get 3 + 23

Remove 3 with 23! Get more than 3 5

Remove 2 with 5! Get 2 more than 1

Remove 1 with 1! The result is that there are three numbers in the range of 1 to 0, and the smaller the number is 4.

So the first is 4.

There are three numbers that are smaller than 4, but 4 has already appeared before, so it is 5 (because 4 has appeared before, so the actual number smaller than 5 is 3)

There are two smaller numbers than it: 3

The number of one smaller than it is 2.

The last number can only be 1

So the number is 45321

C ++ code:

/* Input1 51 2 3 51 51 2 3 5 42 511 54 5 3 2 12 595 OutputCantor = 0 Cantor = 1 Series: 1 2 3 5 4 Cantor = 95 series: 4 5 3 2 1 */# include <iostream> # include <cstdio> # include <string> # include <cmath> # include <queue> # include <stack> # include <map> # include <cstring> # include <algorithm> # define rep (I, a, B) for (int I = (a); I <(B); I ++) # define rev (I, a, B) for (int I = (a); I >= (B); I --) # define clr (a, x) memset (a, x, sizeof a) # define inf 0x3f3f3f3f Typedef long LL; using namespace std; const int mod = 1e9 + 7; const int maxn = 2005; const int maxm = 4005; int fac [] =, 24,120,720,504, 362880,}; int cantor (int * s, int n) {int num = 0; for (int I = 0; I <n-1; I ++) {int tmp = 0; for (int j = I + 1; j <n; j ++) if (s [j] <s [I]) tmp ++; num + = fac [n-i-1] * tmp;} return num;} void _ cantor (int * s, int n, int x) {bool tmp [n + 1]; clr (tmp, 0); for (int I = n-1; I> = 0; I --) {int k = x/fac [I]; x % = fac [I]; int j = 1; for (int sum = 0; sum <k | tmp [j]; j ++) if (! Tmp [j]) sum + +; s [n-1-i] = j; tmp [j] = 1 ;}} int main () {int flag, n; while (~ Scanf ("% d", & flag, & n) {if (flag = 1) {int s [n]; for (int I = 0; I <n; I ++) scanf ("% d", & s [I]); printf ("Cantor = % d \ n", cantor (s, n);} else if (flag = 2) {int x, ans [n]; scanf ("% d", & x); _ cantor (ans, n, x); cout <"sequence:"; for (int I = 0; I <n; I ++) printf ("% d % c ", ans [I], I = n-1? '\ N': '') ;}} return 0 ;}


Reference: http://www.cnblogs.com/hxsyl/archive/2012/04/11/2443009.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.