Cod sursa(job #3147960)

Utilizator vladburacBurac Vlad vladburac Data 28 august 2023 15:45:10
Problema Numerele lui Stirling Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200;
using ll = long long;

/*
Numerele lui Stirling de speta I:
  fie (x)n = x * ( x - 1 ) * ( x - 2 ) * ... * ( x - n + 1 )
  atunci (x)n = suma din ( s(n, k) * x^k )

  mai exista si unsigned Stirling numbers, definite ca coeficientii lui x * ( x + 1 ) * ( x + 2 ) * ... * ( x + n - 1 )
  si astea sunt egale cu numarul de permutari de n numere care au k cicli
  daca le notam cu [n, k], atunci s(n, k) = (-1)^(n-k) * [n, k]
*/

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

ll stirling1[NMAX+1][NMAX+1];
ll stirling2[NMAX+1][NMAX+1];
int main() {
  int i, j, n, k, kind, q;
  stirling1[0][0] = stirling2[0][0] = 1;
  for( i = 1; i <= NMAX; i++ ) {
    stirling1[i][0] = stirling2[i][0] = 0;
    for( j = 1; j < i; j++ ) {
      stirling1[i][j] = -( i - 1 ) * stirling1[i-1][j] + stirling1[i-1][j-1];
      stirling2[i][j] = stirling2[i-1][j-1] + j * stirling2[i-1][j];
    }
    stirling1[i][i] = stirling2[i][i] = 1;
  }
  fin >> q;
  while( q-- ) {
    fin >> kind >> n >> k;
    if( kind == 1 )
      fout << stirling1[n][k] << "\n";
    else
      fout << stirling2[n][k] << "\n";
  }
  return 0;
}