Cod sursa(job #2955903)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 18 decembrie 2022 09:29:37
Problema Numerele lui Stirling Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.18 kb
#include <bits/stdc++.h>
using namespace std;

template <typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template <typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
using i64 = long long int;

const int INF = INT_MAX, MOD = 98999;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {0, 0, 0, -1, 1, -1, 1, 1, -1};
const int dy[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};

struct Stirling {
  vector<vector<i64>> first_order, second_order;
  int max_n, max_m;

  Stirling(int _max_n = 0, int _max_m = 0) {
    init(_max_n, _max_m);
  }

  void init(int _max_n, int _max_m) {
    max_n = _max_n;
    max_m = _max_m;
    first_order.resize(_max_n + 1);
    second_order.resize(_max_n + 1);
    for (auto &vec: first_order)
      vec.resize(_max_m + 1);
    for (auto &vec: second_order)
      vec.resize(_max_m + 1);
  }

  void compute_stirling() {
    first_order[0][0] = 1;
    first_order[1][1] = 1;
    for (i64 i = 2; i <= max_n; i++) {
      for (i64 j = 1; j <= i; j++) {
        first_order[i][j] = (first_order[i - 1][j - 1] - (i - 1) * first_order[i - 1][j]) % MOD;
      }
    }

    second_order[0][0] = 1;
    second_order[1][1] = 1;
    for (i64 i = 2; i <= max_n; i++) {
      for (i64 j = 1; j <= i; j++) {
        second_order[i][j] = (second_order[i - 1][j - 1] + j * second_order[i - 1][j]) % MOD;
      }
    }
  }

  i64 get_first_order(int n, int m) {
    return first_order[n][m];
  }

  i64 get_second_order(int n, int m) {
    return second_order[n][m];
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  /// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  ifstream cin("stirling.in");
  ofstream cout("stirling.out");

  Stirling st = Stirling(200, 200);
  st.compute_stirling();

  int tests; cin >> tests;
  while (tests--) {
    int order, n, m;
    cin >> order >> n >> m;
    if (order == 1) {
      cout << st.get_first_order(n, m) << "\n";
    } else {
      cout << st.get_second_order(n, m) << "\n";
    }
  }

  return 0;
}