Cod sursa(job #3233259)

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

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

vector<vector<long long>> stirling_first(MAXN, vector<long long>(MAXN, 0));
vector<vector<long long>> stirling_second(MAXN, vector<long long>(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;
            stirling_second[n][m] = (stirling_second[n-1][m-1] + m * stirling_second[n-1][m]) % 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) {
            if (m > n) {
                outfile << "0" << endl;
            } else {
                outfile << stirling_first[n][m] << endl;
            }
        } else {
            if (m > n) {
                outfile << "0" << endl;
            } else {
                outfile << stirling_second[n][m] << endl;
            }
        }
    }

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

    return 0;
}