Maruf_Hasan

divisors solution

Oct 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /*
  2. * Sai Cheemalapati
  3. * UVa 294: Divisors
  4. *
  5. */
  6.  
  7. #include<bitset>
  8. #include<cstdio>
  9. #include<vector>
  10.  
  11. using namespace std;
  12.  
  13. int n, l, h;
  14. bitset<10000010> bs;
  15. vector<int> primes;
  16.  
  17. void sieve(long long upper_bound) {
  18. bs.set();
  19. bs[0] = bs[1] = 0;
  20. for(long long i = 2; i <= upper_bound + 1; i++) {
  21. if(bs[i]) {
  22. for(long long j = i * i; j <= upper_bound + 1; j += i)
  23. bs[j] = 0;
  24. primes.push_back((int) i);
  25. }
  26. }
  27. }
  28.  
  29. int number_of_factors(int n) {
  30. int pf_index = 0;
  31. int pf = primes[pf_index];
  32. int result = 1;
  33.  
  34. while(pf * pf <= n) {
  35. int v = 0;
  36. while(n % pf == 0) {
  37. v++;
  38. n /= pf;
  39. }
  40. pf = primes[++pf_index];
  41. result *= (v + 1);
  42. }
  43. if(n != 1) result *= 2;
  44.  
  45. return result;
  46. }
  47.  
  48. int main() {
  49. sieve(35000);
  50. scanf("%d", &n);
  51. while(n--) {
  52. scanf("%d %d", &l, &h);
  53. int best = 0, best_index = 0;
  54. for(int i = l; i <= h; i++) {
  55. int j = number_of_factors(i);
  56. if(j > best) {
  57. best = j;
  58. best_index = i;
  59. }
  60. }
  61. printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, h, best_index, best);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment