【算法课】最多约数问题

题目描述

正整数 x 的约数是能整除x的正整数,其约数的个数记为div(x),例如div(10)=4。设 a 和 b 是两个正整数,找出 a 和 b 之间(包含a,b)约数个数最多的数 x 的约数个数

 

输入

两个正整数a和b,(1<=a<=b<=1e5)

 

输出

一个正整数表示答案。

 

样例输入

1 36

 

样例输出

9

 


 

 

【方法1】线性筛

 1 /*
 2     Test1 : 线性筛的做法
 3 */
 4 #include<bitset>
 5 #include<cstdio>
 6 #include<iostream>
 7 using namespace std;
 8 typedef long long ll;
 9 const int N = 1e6+10;
10  
11 int Prime[N],Cnt[N];
12 bool is_prime[N];
13 int A,B,Ans=2;
14  
15 int Count_Prime ( int x , int p ){
16     int tot = 0 ;
17     while( x % p == 0 ){
18         tot ++ ;
19         x /= p ;
20     }
21     return tot ;
22 }
23  
24 void Is_prime(){
25     //memset( is_prime , 0 , sizeof(is_prime) );
26     for(int i=1;i<N;i++) Cnt[i] = 1 ;
27     for(int i=2;i<N;i++){
28         if( !is_prime[i] ){
29             for(int j=i*2 ; j < N ; j+=i ){
30                 Cnt[j] *= ( Count_Prime( j , i ) + 1 ) ;
31                 is_prime[j] = true ;
32                 if( A <= j && j <= B ){
33                     if( Ans < Cnt[j] )
34                         Ans = Cnt[j] ;
35                 }
36             }
37         }
38     }
39 }
40 int main()
41 {
42     scanf("%d%d",&A,&B);
43     Is_prime() ;
44     /*
45     for(int i=A;i<=B;i++){
46         printf("%d , %d\n",i,Cnt[i]);
47     }
48     */
49     if( A == B && A == 1 ) Ans = 1 ;
50     printf("%d\n",Ans);
51     return 0;
52 }
View Code

 

【方法2】搜索

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5+10;
 4 int prime[] = { 0,2,3,5,7,11,
 5                 13,17,19,23,29,
 6                 31,37,41,47,51};
 7 int A , B ;
 8 int Ans = 2 ;
 9 
10 int dfs( int Num , int pos ,int k , int Cnt ){
11 
12     if( pos > 15 ) return 2;
13     int res = Cnt;
14     for(int i=1;i<=k;i++){
15         if( B/prime[pos] < Num ) break ;
16         Num *= prime[pos] ;
17         res = max( res , dfs( Num ,pos+1 , i , Cnt*(i+1) ) );
18     }
19     if( A <= Num && Num <= B ){
20         if( Cnt > Ans ) Ans = Cnt ;
21     }
22 
23     return res;
24 }
25 
26 int main()
27 {
28     scanf("%d%d",&A,&B);
29     if( !(A^1) && A == B ){
30         Ans = 1 ;
31     }else{
32         dfs( 1 , 1 ,64 , 1 );
33     }
34     printf("%d\n",Ans);
35     return 0 ;
36 }
View Code

 

posted @ 2019-08-31 10:02  Osea  阅读(1288)  评论(0编辑  收藏  举报