Cod sursa(job #1741757)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 14 august 2016 23:36:50
Problema Expresii 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <iomanip>
#include <queue>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <set>
#include <map>
using namespace std;

ifstream cin("expresii2.in");
ofstream cout("expresii2.out");

const int SIGMA = 26;
const int MAXN = 30;

long long dp[1 + MAXN][1 + MAXN][SIGMA + 3];

void Print(int n, int k, long long p) {
    if (n <= 0)
        return;
    int i = 0;
    while (dp[n][k][i] < p)
        i++;
    if (i != 0)
        p -= dp[n][k][i - 1];
    if (i < SIGMA) {
        k++;
        char letter = 'A' + i;
        cout << letter;
    }
    if (i == SIGMA || i == SIGMA + 1)
        k--;
    if (i == SIGMA)
        cout << "+";
    if (i == SIGMA + 1)
        cout << "*";
    if (i ==SIGMA + 2)
        cout << "!";
    Print(n - 1, k, p);
}

int main() {
    int n, k;
    long long p;
    cin >> n >> k >> p;
    for (int i = 0; i < SIGMA + 3; i++)
        dp[0][0][i] = 1;
    for (int i = 1; i <= n - 1; i++)
        for (int j = 0; j <= i; j++) {
            for (int l = 0; l < k; l++)
                dp[i][j][l] = dp[i - 1][j + 1][SIGMA + 2];
            dp[i][j][SIGMA + 2] = dp[i - 1][j][SIGMA + 2];
            if (j > 0) {
                dp[i][j][SIGMA] = dp[i - 1][j - 1][SIGMA + 2];
                dp[i][j][SIGMA + 1] = dp[i - 1][j - 1][SIGMA + 2];
            }
            for (int l = 1; l < SIGMA + 3; l++)
                dp[i][j][l] += dp[i][j][l - 1];
        }
    cout << (long long) k *dp[n - 1][0][SIGMA + 2] << "\n";
    char letter = (p - 1) / dp[n - 1][0][SIGMA + 2] + 'A';
    cout << letter;
    p = (p - 1) % dp[n - 1][0][SIGMA + 2] + 1;
    Print(n - 1, 0, p);
    return 0;
}