Cod sursa(job #2556540)

Utilizator gabbie02Thomits Gabriel gabbie02 Data 24 februarie 2020 23:26:07
Problema Numerele lui Stirling Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>

using namespace std;

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

int str1[201][201];
unsigned int str2[201][201];

int stirl_1(unsigned short int n, unsigned short int m)
{
    if(m > n){
        return 0;
    }
    if(m == n){
        return 1;
    }
    if(m == 0){
        return 0;
    }
    if(str1[n][m] != 0){
        return str1[n][m];
    }
    str1[n][m] = ((stirl_1(n - 1, m - 1) % 98999) - (((n - 1) * (stirl_1(n - 1, m) % 98999)) % 98999) % 98999) % 98999;
    return str1[n][m];
}

unsigned int stirl_2(unsigned short int n, unsigned short int m)
{
    if(n == 0 && m == 0){
        return 1;
    }
    if(m > n || n == 0 || m == 0){
        return 0;
    }
    if(str2[n][m] != 0){
        return str2[n][m];
    }
    str2[n][m] = ((stirl_2(n - 1, m - 1) % 98999) + ((m * (stirl_2(n - 1, m)) % 98999) % 98999) % 98999) % 98999;
    return str2[n][m];
}

int main()
{
    unsigned short int t;
    unsigned short int n, m;
    char op;
    fin >> t;
    while(t){
        fin >> op >> n >> m;
        switch(op){
            case '1': fout << stirl_1(n, m) << '\n';
            break;
            case '2': fout << stirl_2(n, m) << '\n';
            break;
        }
        t--;
    }
    return 0;
}