Cod sursa(job #3344873)

Utilizator rrfeierFeier Raul rrfeier Data 6 martie 2026 11:51:30
Problema Numerele lui Stirling Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
#include <fstream>

using namespace std;
const int MOD = 98999;

long long stirling_1(int N, int K) {
  vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, 0));
  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = (dp[i - 1][j - 1] + (i - 1) * dp[i - 1][j] % MOD) % MOD;
    }
  }

  return dp[N][K];
}

long long stirling_2(int N, int K) {
  vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, 0));
  dp[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= i; j++) {
      dp[i][j] = (dp[i - 1][j - 1] + j * dp[i - 1][j] % MOD) % MOD;
    }
  }

  return dp[N][K];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ifstream cin{"stirling.in"};
  ofstream cout{"stirling.out"};

  int tc;
  cin >> tc;

  while (tc--) {
    int c, a, b;
    cin >> c >> a >> b;

    int ok = 1;
    if (a > b) {
      swap(a, b);
      ok = -1;
    }

    long long res = 0;
    if (c == 1) {
      res = stirling_1(a, b);

    } else
      res = stirling_2(a, b);

    res *= ok;

    cout << res << '\n';
  }

  return 0;
}