close

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=278&page=show_problem&problem=3757

 

#include <iostream>

#include <string>

#include <cstring>

#define LEN 21

 

using namespace std;

 

int main() {

    int dataNum;    

    long long int k, factorial[LEN];

    string s;

    char *ans;

 

    factorial[0] = 1;

    for(int i = 1; i < LEN; i++)

        factorial[i] = factorial[i - 1] * i;

 

    cin >> dataNum;    

    for(int d = 0; d < dataNum; d++) {

        cin >> s >> k;    

 

        ans = new char [s.length() + 1];

        memset(ans, '-', sizeof(char) * s.length());

        ans[s.length()] = '\0';

 

        for(int i = 0; i < s.length(); i++) {

            long long int stage;    

            int place = 0;    

 

            stage = k / factorial[s.length() - i - 1];

            if(k % factorial[s.length() - i - 1] == 0)

                stage--;

 

            for(int count = 0; count <= stage; place++) {

                if(ans[place] != '-')

                    continue;

                count++;

            }

            ans[place - 1] = s[i];

 

            k = k % factorial[s.length() - i - 1];

            if(k == 0)

                k = factorial[s.length() - i - 1];

        }

 

        cout << "Case " << d + 1 << ": " << ans << endl;

        delete [] ans;

    }

 

    return 0;

}

arrow
arrow
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()