HDU 1027 Ignatius and the Princess II 全排列

题意: 1n ,求第 m 个全排列

思路:因为,m10000,所以只需要求8的全排列, 8!=40320 已经大于10000了,其他的 n>8 全部可以根据 n=8 推出来。我取巧用了STL的next_permutation()函数,万能的STL。

http://acm.hdu.edu.cn/showproblem.php?pid=1027

/*********************************************
    Problem : HDU 1027
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int a[10005][9]; //最多只要8个数的全排列

void init() {
    int d[] = {1,2,3,4,5,6,7,8};
    rep(i,1,10000) {
        rep(j,1,8) {
            a[i][j] = d[j-1];
        }
        next_permutation(d,d+8);
    }
}

void input() {

}

void solve() {
    if(n <= 8) {
        int cha = 8 - n;
        rep(i,cha+1,8) {
            printf("%d",a[m][i]-cha);
            if(i == 8) puts("");
            else printf(" ");
        }
    }
    else {
        int cha = n - 8;
        rep(i,1,n-8) printf("%d ",i);
        rep(i,1,8) {
            printf("%d",a[m][i]+cha);
            if(i == 8) puts("");
            else printf(" ");
        }
    }
}   

int main(void) {
    //freopen("a.in","r",stdin);
    //scanf("%d",&T); while(T--) {
    init();
    while(~scanf("%d %d",&n,&m)) {
    //while(scanf("%d",&n),n) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值