题意:
求区间[L, U]的正因数的个数。
分析:
事先打好素数表,按照上面的公式统计出最大值即可。
1 #include <cstdio> 2 #include <cmath> 3 4 const int maxn = 31700; 5 bool vis[maxn + 10]; 6 int prime[3450], cnt = 0; 7 8 void Init() 9 { 10 int m = sqrt(maxn + 0.5); 11 for(int i = 2; i <= m; ++i) if(!vis[i]) 12 for(int j = i * i; j <= maxn; j += i) vis[j] = true; 13 for(int i = 2; i <= maxn; ++i) if(!vis[i]) prime[cnt++] = i; 14 } 15 16 int factor_number(int n) 17 { 18 int m = sqrt(n + 0.5); 19 int ans = 1; 20 for(int i = 0; prime[i] <= m; ++i) 21 { 22 int k = 1; 23 while(n % prime[i] == 0) 24 { 25 k++; 26 n /= prime[i]; 27 } 28 ans *= k; 29 } 30 if(n > 1) ans <<= 1; 31 return ans; 32 } 33 34 int main() 35 { 36 Init(); 37 int a, b, T; 38 scanf("%d", &T); 39 while(T--) 40 { 41 scanf("%d%d", &a, &b); 42 int ans, temp = 0; 43 for(int i = a; i <= b; ++i) 44 { 45 int k = factor_number(i); 46 if(k > temp) { temp = k; ans = i; } 47 } 48 printf("Between %d and %d, %d has a maximum of %d divisors. ", a, b, ans, temp); 49 } 50 51 return 0; 52 }