Cod sursa(job #3357633)

Utilizator Andrei_GhiocelAndrei Tiberiu Ghiocel Andrei_Ghiocel Data 12 iunie 2026 12:18:35
Problema Numerele lui Stirling Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int MOD = 98999;
const int MAX = 205;

//stocare nr.stirling
int s1[MAX][MAX]; 
int s2[MAX][MAX]; 
void precompute() {
    s1[0][0] = 1;
    s2[0][0] = 1;

    for (int n = 1; n <= 200; n++) {
        s1[n][0] = 0;
        s2[n][0] = 0;
        for (int m = 1; m <= n; m++) {
            // s(n,m) = s(n-1,m-1) - (n-1)*s(n-1,m)
            long long val1 = (s1[n - 1][m - 1] - 1LL * (n - 1) * s1[n - 1][m]) % MOD;
            s1[n][m] = (val1 + MOD) % MOD; 

            // S(n,m) = S(n-1,m-1) + m*S(n-1,m)
            long long val2 = (s2[n - 1][m - 1] + 1LL * m * s2[n - 1][m]) % MOD;
            s2[n][m] = val2;
        }
    }
}

int main() {
    ifstream fin("stirling.in");
    ofstream fout("stirling.out");

    if (!fin || !fout) return 0;

    precompute();

    int T;
    fin >> T;

    while (T--) {
        int x, n, m;
        fin >> x >> n >> m;

        if (m > n || m < 0 || n < 0 || n > 200 || m > 200) {
            fout << 0 << "\n";
            continue;
        }

        if (x == 1) {
            fout << s1[n][m] << "\n";
        }
        else {
            fout << s2[n][m] << "\n";
        }
    }

    return 0;
}