Codeforces 747F Igor and Interesting Numbers dp

Igor and Interesting Numbers

枚举每一位, 用dp去算方案数。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 20 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

LL F[18];
LL dp[16][18];

int c[16];
int ans[17];

int k, t;

LL getDp(int n, int *c) {
    if(n == 0) return 1;
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i <= c[0]; i++) dp[0][i] = 1;
    for(int i = 0; i < 15; i++) {
        for(int j = 0; j <= n; j++) {
            for(int k = 0; k <= c[i + 1]; k++) {
                if(j + k > n) continue;
                dp[i + 1][j + k] += F[j + k] / F[j] / F[k] * dp[i][j];
            }
        }
    }
    return dp[15][n];
}

char print(int x) {
    if(x <= 9) return '0' + x;
    else return 'a' + (x - 10);
}

int main() {
    for(int i = F[0] = 1; i <= 17; i++) F[i] = F[i - 1] * i;
    scanf("%d%d", &k, &t);
    for(int i = 0; i <= 15; i++) c[i] = t;
    for(int len = 1; len <= 17; len++) {
        LL tot = 0;
        for(int j = 1; j <= 15; j++) {
            if(!c[j]) continue;
            c[j]--;
            int way = getDp(len - 1, c);
            if(way < k) k -= way, c[j]++, tot += way;
            else {
                ans[1] = j;
                break;
            }
        }
        if(!ans[1]) continue;
        for(int i = 2; i <= len; i++) {
            for(int j = 0; j <= 15; j++) {
                if(!c[j]) continue;
                c[j]--;
                int way = getDp(len - i, c);
                if(way < k) k -= way, c[j]++;
                else {
                    ans[i] = j;
                    break;
                }
            }
        }
        for(int i = 1; i <= len; i++) printf("%c", print(ans[i]));
        puts("");
        return 0;
    }
    return 0;
}

/*
*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/10930100.html
今日推荐