HDU 1027 Ignatius and the Princess II(全排列)

题意: 对于给定的 1-n的序列,输出第m个全排列
思路: 注意到m最多只有10000,故最多只需要改变末尾的k个,对末尾的k个进行全排列,然后顺序输出

//#include <iostream>
//#include <cstdio>
//#include <string>
//#include <cstring>
//#include <string.h>
//#include <cmath>
//#include <algorithm>
//#include <cctype>
//#include <cstdlib>
//#include <ctime>
//#include <queue>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

ll dp[50];

int main()
{


    int n, m;
    while (scanf("%d%d", &n, &m) != EOF)
    {
        int all = 1; int p = 1;
        while (all < m)++p,all *= p;
        vector<int>v;
        for (int i = n - p + 1; i <= n; i++)v.push_back(i);
        do
        {
            m--;
            if (m == 0)break;
        } while (next_permutation(v.begin(), v.end()));
        int flag = 0;
        for (int i = 1; i <= n - p; i++)
        {
            if (!flag)flag = 1;
            else printf(" ");
            printf("%d", i);
        }
        for (int i = 0; i < v.size(); i++)
        {
            if (!flag)flag = 1;
            else printf(" ");
            printf("%d", v[i]);
        }
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值