fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int fact(int n, int p) {
  5. if (n ==0 || n==1) {
  6. return 1;
  7. } else {
  8. return n*fact(n-1,p) % p;
  9. }
  10. }
  11.  
  12. int main() {
  13. int t,p,n;
  14. cin >> t;
  15. for (int i=0; i<t; i++) {
  16. cin >> n >> p;
  17. cout << fact(n,p) << endl;
  18. }
  19. return 0;
  20. }
Success #stdin #stdout 0s 4404KB
stdin
3
2 5
5 11
21 71
stdout
2
10
6