小易的字典, 网易笔试题

import java.util.*;
public class Main{
    static long comb(int n, int m, int k) {
        long res = 1L;
        for(int i=1; i <= m; i++) {
            res = res*(n-m+i)/i;
            if(res > k) break;
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
        
        if(comb(m+n, m, k) < k) System.out.println(-1);
        else {
            char[] res = new char[m+n];
            int idx = 0;
            while(n != 0 && m != 0) {
                long tmp = comb(n+m-1, m, k);
                if(tmp < k) {
                    k -= tmp;
                    res[idx++] = 'z';
                    m --;
                } else {
                    res[idx++] = 'a';
                    n --;
                }
            }
            for(int i=0; i < n; i++) res[idx++] = 'a';
            for(int j=0; j < m; j++) res[idx++] = 'z';
            System.out.println(String.valueOf(res));
        }
    }
}

你可能感兴趣的:(小易的字典, 网易笔试题)