题意:输入两个整数L,U(L <= U <= 1000000000, u - l <= 10000),统计区间【L,U】的整数中哪一个的正约数最多,多个输出最小的那个
本来想着用欧拉函数,打个表求所有的约数个数,但是u太大,直接暴力求解
利用唯一分解定理,刷选出根号1000000000的素数,对l,u区间的每一个数进行分解
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int Max = 34000; 7 int prime[Max + 5],total,flag[Max]; 8 void get_prime() 9 { 10 total = 0; 11 for(int i = 2; i <= Max; i++) 12 { 13 if(flag[i] == 0) 14 { 15 prime[++total] = i; 16 for(int j = i; j <= Max / i; j++) 17 { 18 flag[i * j] = 1; 19 } 20 } 21 } 22 } 23 int get_gcd(int n) 24 { 25 int num = 0,ans = 1; 26 for(int i = 1; i <= total; i++) 27 { 28 if(prime[i] > n) 29 break; 30 if(n % prime[i] == 0) 31 { 32 num = 0; 33 while(n % prime[i] == 0) 34 { 35 num++; 36 n = n / prime[i]; 37 } 38 ans *= (num + 1); 39 } 40 } 41 if(n > 1) 42 { 43 ans *= 2; 44 } 45 return ans; 46 } 47 int main() 48 { 49 int l,u,test; 50 get_prime(); 51 scanf("%d", &test); 52 while(test--) 53 { 54 scanf("%d%d", &l, &u); 55 int temp; 56 int maxn = 0, p; 57 for(int i = l; i <= u; i++) 58 { 59 temp = get_gcd(i); 60 if(temp > maxn) 61 { 62 maxn = temp; 63 p = i; 64 } 65 } 66 printf("Between %d and %d, %d has a maximum of %d divisors. ", l, u, p, maxn); 67 } 68 return 0; 69 }