Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CPP/algorithms/mathematical/derangements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Derangements Calculation using Inclusion-Exclusion Principle
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
#include <iostream>
using namespace std;

int main() {
int n, m;
cin >> n >> m; // Read n (limit) and m (modulus)

long long c = 1; // Initialize c = 1 (use long long to avoid overflow before mod)
for (int i = 1; i <= n; i++) {
// Multiply c by i under modulo m
c = (c * i) % m;

// Add or subtract 1 depending on whether i is odd or even
if (i % 2 == 1) c = (c - 1 + m) % m; // subtract 1 safely under mod
else c = (c + 1) % m; // add 1 safely under mod

cout << c << ' ';
}
cout << endl;
return 0;
}