全排列散列

题目描述

       Did you remember that simple problem permutation of five? Now, TY has meeted another problem about permutation. Help him.
       You are given two numbers, n and m. The permutation is based on n digits. 
       First, you should point out the m th permutation of all permutations in an ascend order. Than reverse that permutation and print the rank of that reversed permutation of all permutations in an ascend order.

输入

Multiple cases( > 1000). Each case contains a line with two integer, n,m. (1 <= n <= 9, 1 <= m <= n!)

输出

Each case a line, the mth permutation and the rank, seperated by a space.

样例输入

5 1
5 10

样例输出

12345 120
13452 48

#include<iostream>
using namespace std;
int n, m;
bool used[10];
int a[9];
int fac[10];
void  factorial(){
	int i;
	fac[0] = 1;
	for (i = 1; i < 10; i++)
		fac[i] = i*fac[i - 1];
}
int getNum(int i){
	int j,k=0;
	for (j = 1; k<i;j++)
	if (used[j] == false)k++;
	return j-1;
}
void find(){
	int i;
	for (i = 0; i < n; i++){
		a[i]=getNum( m / fac[n - 1-i]+1);
		used[a[i]] = true;
		m %= fac[n-i - 1];
	}
}
void turn(){
	int i, j;
	for (i = 0; i * 2 < n; i++){
		j = a[i];
		a[i] = a[n - i-1];
		a[n - i-1] = j;
	}
}
int which(int n){
	int i, j=0;
	for (i = 0; i < n; i++)
	{
		if (used[i] == false)j++;
	}
	return j;
}
int toNum(){
	int i, k = 0;
	for (i = 0; i < n; i++){
		k += fac[n-i-1] * (which(a[i])-1);
		used[a[i]] = true;
	}
	return k;
}
int main(){
	//freopen("in.txt", "r", stdin);
	factorial();
	while (scanf("%d%d",&n,&m)!=-1){
		memset(used, 0, sizeof(used));
		m--;
		find();
		int i;
		for (i = 0; i < n; i++)
			 printf("%d",a[i]);
		cout << " ";
		turn();
		memset(used, 0, sizeof(used));
		printf("%d\n", toNum() + 1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值