Cod sursa(job #3233258)

Utilizator MirceaDonciuLicentaLicenta Mircea Donciu MirceaDonciuLicenta Data 2 iunie 2024 21:00:38
Problema Numerele lui Stirling Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

const int MOD = 98999;
const int MAXN = 201; // maximum value for n and m based on the problem statement

vector<vector<int>> stirling_first(MAXN, vector<int>(MAXN, 0));
vector<vector<int>> stirling_second(MAXN, vector<int>(MAXN, 0));

void compute_stirling_numbers() {
    stirling_first[0][0] = 1;
    stirling_second[0][0] = 1;

    for (int n = 1; n < MAXN; ++n) {
        for (int m = 1; m <= n; ++m) {
            stirling_first[n][m] = (stirling_first[n-1][m-1] + (n-1) * stirling_first[n-1][m] % MOD) % MOD;
            stirling_second[n][m] = (stirling_second[n-1][m-1] + m * stirling_second[n-1][m] % MOD) % MOD;
        }
    }
}

int main() {
    ifstream infile("stirling.in");
    ofstream outfile("stirling.out");

    int T;
    infile >> T;

    compute_stirling_numbers();

    while (T--) {
        int x, n, m;
        infile >> x >> n >> m;
        if (x == 1) {
            outfile << stirling_first[n][m] << endl;
        } else {
            outfile << stirling_second[n][m] << endl;
        }
    }

    infile.close();
    outfile.close();

    return 0;
}