Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. int N[100000] = { ... }
  2. int D[100000] = { ... }
  3.  
  4. for (int i = 0; i < 100000; i++)
  5. for (int j = 0; j < 100000; j++)
  6. {
  7. int k = gcd(N[i], D[j]); // euclids algorithm
  8.  
  9. N[i] /= k;
  10. D[j] /= k;
  11. }
  12.  
  13. // Not very optimised, one could easily leave out the even numbers, or also the multiples of 3
  14. // to reduce space usage and computation time
  15. int *spf_sieve = malloc((limit+1)*sizeof *spf_sieve);
  16. int root = (int)sqrt(limit);
  17. for(i = 1; i <= limit; ++i) {
  18. spf_sieve[i] = i;
  19. }
  20. for(i = 4; i <= limit; i += 2) {
  21. spf_sieve[i] = 2;
  22. }
  23. for(i = 3; i <= root; i += 2) {
  24. if(spf_sieve[i] == i) {
  25. for(j = i*i, step = 2*i; j <= limit; j += step) {
  26. if (spf_sieve[j] == j) {
  27. spf_sieve[j] = i;
  28. }
  29. }
  30. }
  31. }
  32.  
  33. N[i]=p1^kn1 * p2^kn2 ...
  34. D[i]=p1^kd1 * p2^kd2 ...
  35.  
  36. Power_N[p1]=sum of kn1 for all i's
  37. Power_D[p1]=sum of kd1 for all i's
  38.  
  39. Divide the N and D by p1^(min(Power_N[p1],Power_D[p2])) in O(10^5) each
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement