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

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

//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
//#include 
#include 

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");
    }
}

你可能感兴趣的:(HDOJ)