最多约束问题

问题描述: 正整数x 的约数是能整除x 的正整数。正整数x的约数个数记为div(x)。例如,1,2, 5,10 都是正整数10的约数,且div(10)=4。设a 和b是2 个正整数,a≤b,找出a 和b之间 约数个数最多的数x。

算法设计: 对于给定的2 个正整数a <= b 计算a 和b之间约数个数最多的数。 可以保证a和b都不超过2000000.

数据输入: 输入数据有2个正整数a和b。

结果输出: 若找到的a 和b之间约数个数最多的数是x,将div(x)输出。

Sample Input

1 36

Sample Output

9

 1 #include<iostream> 
 2 #include<algorithm> 
 3 #include<stdlib.h> 
 4 #include<cmath> 
 5 #include<string> 
 6 #define MAX 2000010 
 7 using namespace std; 
 8 
 9 const double pi = 4.0 * atan(1.0); 
10 typedef unsigned long long uLL; 
11 typedef signed long long LL; 
12 
13 int flag[MAX], cnt[MAX]; 
14 
15 int cal(int i, int x) 
16 { 
17     x = x/i; 
18     int cnt = 1; 
19     while(x % i == 0) 
20     { 
21         x /= i; 
22         cnt ++; 
23     } 
24     return cnt; 
25 } 
26 
27 void set() 
28 { 
29     for(int i = 0;i <= MAX;i ++) 
30         cnt[i] = 1; 
31     flag[0] = flag[1] = 1; 
32     for(int i = 2;i <= MAX;i ++) 
33     { 
34         if(!flag[i]) 
35         { 
36             for(int j = 2;i*j <= MAX;j ++) 
37             { 
38                 flag[i*j] = 1; 
39                 cnt[i*j] *= (cal(i, i*j)+1); 
40             } 
41         } 
42     } 
43 } 
44 
45 int main() 
46 { 
47     set(); 
48     int a, b; 
49     while(cin >> a >> b) 
50     { 
51         int max = 0; 
52         for(int i = a;i <= b;i ++) 
53         { 
54             if(cnt[i] > max) 
55                 max = cnt[i]; 
56         } 
57         if(max == 1) 
58             max ++; 
59         printf("%d\n", max); 
60     } 
61 }

 

转载于:https://www.cnblogs.com/shichuanwang/archive/2012/11/09/2763436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值