Advertisement
audreych

12908 - Number of factorial’s factors

Dec 30th, 2020 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int cnt[100000];
  4. int prime[100000];
  5.  
  6. int main(){
  7.     int N;
  8.     scanf("%d", &N);
  9.     for(int i = 2; i <= N; i++){
  10.         for(int j = 2; j <= i; j++){
  11.             if(i % j == 0 && i!= j){
  12.                 // a number is not a prime if it can be divided by other number, so just break it
  13.                 prime[i] = 0;
  14.                 break;
  15.             } else {
  16.                 // else it is a prime, because it can only be divided by its own number
  17.                 prime[i] = 1;
  18.             }
  19.         }
  20.         // printf("i = %d, prime = %d\n", i, prime[i]);
  21.     }
  22.    
  23.     int temp = 0; // store i so that i can iterate how many time it can be divided by j (the count)
  24.     int total = 0;
  25.     for(int i = N; i >= 2; i--){
  26.         temp = i;
  27.         for(int j = 2; j <= N; j++){
  28.         // got (6/7) cuz < not <=, it is <= because it applies for 2! and 3!
  29.         // because 2! is 2x1 which is a prime number if i didn't put <= it won't iterate the 2
  30.             if(prime[j] == 1){ //if prime
  31.                 while(temp >= j && temp % j == 0){
  32.                     // iterate if temp is still more than the prime number, and if it's divisible by the number
  33.                     // e.g 4/2 = 2, 2/2 = 1 -> iterate this
  34.                     temp = temp / j;
  35.                     cnt[j]++;
  36.                     //printf("cnt[%d] = %d\n", j, cnt[j]);
  37.                 }
  38.             }
  39.         }
  40.     }
  41.     unsigned long long final = 1; // variable to store to use at legendre formula
  42.     for(int i = 2; i <= N; i++){
  43.         if(cnt[i] == 0) continue;
  44.         else {
  45.             cnt[i] = cnt[i]+1;
  46.             final *= cnt[i];
  47.             //printf("cnt[%d] = %d\n", i, cnt[i]);
  48.         }
  49.     }
  50.     printf("%llu", final);
  51.     return 0;
  52. }
  53. // notes :
  54. // first, I check for prime and store it in an array with 1 and 0 as the element
  55. // 1 is true and 0 is false
  56. // Then, use loops to iterate N! (not by calculating the results), n, n-1, n-2, n-3, ....., 2, 1
  57. // actually 1 does not matter (so we can just stop the loop when it reaches 2)
  58. // then, each time the loops iterate, we check if the i is divisible right now by prime numbers
  59. // if it's still, then keeps dividing until it reaches 1, or until temp < j
  60. // e.g 4 -> 4/2 = 2 (1st loop) 2/2 = 1 (2nd loop) then loop stops because 1 < 2
  61. // remember to count how many times, I make another array to store how many count in the respective prime number
  62. // then with the legendre formula, make a loop and cnt[i] += 1; then multiply everything
  63. // don't forget that if the count in the loop is zero we can just ignore it, if(cnt[i] == 0) continue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement