fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // Using long long to handle values up to 10^17
  7. typedef long long ll;
  8.  
  9. ll nCr[31][31];
  10.  
  11. // Precompute combinations using Pascal's Triangle
  12. void precompute() {
  13. for (int i = 0; i <= 30; i++) {
  14. nCr[i][0] = 1; // nC0 is always 1
  15. for (int j = 1; j <= i; j++) {
  16. nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j];
  17. }
  18. }
  19. }
  20.  
  21. // Calculate P(n, k) = n! / (n-k)!
  22. ll nPk(int n, int k) {
  23. ll res = 1;
  24. for (int i = 0; i < k; i++) {
  25. res *= (n - i);
  26. }
  27. return res;
  28. }
  29.  
  30. int main() {
  31. precompute();
  32.  
  33. int T;
  34. if (!(cin >> T)) return 0;
  35.  
  36. for (int t = 1; t <= T; t++) {
  37. int n, k;
  38. cin >> n >> k;
  39.  
  40. ll ans = 0;
  41. // If k > n, it's impossible to place rooks without attacking
  42. if (k <= n) {
  43. ans = nCr[n][k] * nPk(n, k);
  44. }
  45.  
  46. cout << "Case " << t << ": " << ans << endl;
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty