Mathematicians love all sorts of odd properties of numbers. For instance, they consider  to be an
interesting number, since it is the first odd number for which the sum of its divisors is larger than the
number itself.
To help them search for interesting numbers, you are to write a program that scans a range of
numbers and determines the number that has the largest number of divisors in the range. Unfortunately,
the size of the numbers, and the size of the range is such that a too simple-minded approach may take
too much time to run. So make sure that your algorithm is clever enough to cope with the largest
possible range in just a few seconds.
Input
The first line of input specifies the number N of ranges, and each of the N following lines contains a
range, consisting of a lower bound L and an upper bound U, where L and U are included in the range.
L and U are chosen such that ≤ L ≤ U ≤ and ≤ U − L ≤ .
Output
For each range, find the number P which has the largest number of divisors (if several numbers tie for
first place, select the lowest), and the number of positive divisors D of P (where P is included as a
divisor). Print the text ‘Between L and H, P has a maximum of D divisors.’, where L, H, P,
and D are the numbers as defined above.
Sample Input Sample Output
Between and , has a maximum of divisors.
Between and , has a maximum of divisors.
Between and , has a maximum of divisors.

题目

/*
1.约数个数定理:对于一个数a可以分解质因数:a=a1的r1次方乘以a2的r2次方乘以a3的r3次方乘以…… 则a的约数的个数就是(r1+1)(r2+1)(r3+1)…… 需要指出来的是,a1,a2,a3……都是a的质因数。r1,r2,r3……是a1,a2,a3……的指数。 2.判断m的约数个数:将m开方得n,判断n之前属于m的约数个数num。若n为整数,则m约数个数为2*num+1,否则为2*num
*/
#include <bits/stdc++.h> using namespace std; int countFactor(int n)
{
int cnt = ;
for(int i=; i<=sqrt(n); i++){
int c = ;
while(n % i == ){
n /= i;
c++;
}
cnt *= (c + );
}
if(n > ) cnt *= ;
return cnt;
} int main()
{
int n, l, u; scanf("%d", &n);
while(n--) {
scanf("%d%d", &l, &u); int ans = ,num;
for(int i=l; i<=u; i++){
int tmp = countFactor(i);
if(tmp > ans){
ans = tmp;
num = i;
}
} printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, u, num, ans);
} return ;
}

Code短除

#include <iostream>
#include <cstdlib> using namespace std; int visit[];
int prime[]; //因式分解,计算因子个数
int number( int a, int n )
{
int sum = ;
for ( int i = ; a > && i < n ; ++ i )
if ( a%prime[i] == ) {
int count = ;
while ( a%prime[i] == ) {
count ++;
a /= prime[i];
}
sum *= count;
}
return sum;
} int main()
{
//利用筛法计算素数,打表
for ( int i = ; i < ; ++ i )
visit[i] = ;
int count = ;
for ( int i = ; i < ; ++ i )
if ( visit[i] ) {
prime[count ++] = i;
for ( int j = *i ; j < ; j += i )
visit[j] = ;
} long a,b,c;
while ( cin >> c )
while ( c -- ) {
cin >> a >> b;
long save = a,max = ,temp;
for ( long i = a ; i <= b ; ++ i ) {
temp = number( i, count );
if ( temp > max ) {
max = temp;
save = i;
}
} cout << "Between " << a << " and " << b << ", " << save
<< " has a maximum of " << max << " divisors.\n";
}
return ;
}

筛法

最新文章

  1. MongoDB的用户管理(6)
  2. T-SQL编程练习(带注释)
  3. Windows Phone的简单学习
  4. SSH公钥认证+优化
  5. redis资料汇总
  6. 添加事件监听兼容IE6-8
  7. OpenLayers访问WTMS服务及添加Googlemap
  8. SQL Server 2008 2005删除或压缩数据库日志的方法
  9. HDU1305 Immediate Decodability(水题字典树)
  10. Javascript数组系列五之增删改和强大的 splice()
  11. sql select中加入常量列
  12. 配置maven从自己的私服下载jar包nexus、maven私服仓库(二)
  13. Redis数据结构之set
  14. Elasticsearch Docker环境下安装
  15. HTML5无刷新修改URL
  16. jquery如何让checkbox如何取消勾选
  17. linux too many open files报错
  18. Selenium Webdriver——Table类封装
  19. pdi vcard-2.1
  20. Oracle 自增长id

热门文章

  1. Hi3518EV300编译U-Boot和内核报错:loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt &lt; (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))&#39; failed. Aborted (core dumped)
  2. JSP自定义tld方法标签
  3. error LNK2001: unresolved external symbol __imp___time64
  4. loj6392 「THUPC2018」密码学第三次小作业 / Rsa
  5. laravel5.2总结--文件上传
  6. Asp.net自定义控件开发任我行(5)-嵌入资源上
  7. Excel动画教程50例(一)
  8. MFC DLL 可以封装MFC的窗体 供别的MFC程序使用
  9. JWT实现token认证
  10. Spring boot 整合jsp、thymeleaf、freemarker