Advertisement
Joao_Joao

Questão 251 B Lista de Exercícios IFPB

Apr 27th, 2022 (edited)
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. static int *memo;
  5. static const int MOD = 1e9+7;
  6.  
  7. unsigned long long factorial (int n) {
  8.   if(n < 2) return 1;
  9.   if(memo[n]) return memo[n];
  10.   return memo[n] = (n * factorial(n-1)) % MOD;
  11. }
  12.  
  13. int main() {
  14.   int n, i;
  15.   printf("Digite o valor de N: ");
  16.   scanf("%d", &n);
  17.   memo = malloc((n+1) * sizeof(int));
  18.   for (i = 0; i <= n; ++i) memo[i] = 0;
  19.   printf("%d! = %lld\n", n, factorial(n));
  20.  
  21.   free(memo);
  22.   memo = NULL;
  23.  
  24.   return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement