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 ;
}

筛法

UVA - 294 Divisors【数论/区间内约数最多的数的约数个数】的更多相关文章

  1. UVA 294 294 - Divisors (数论)

    UVA 294 - Divisors 题目链接 题意:求一个区间内,因子最多的数字. 思路:因为区间保证最多1W个数字,因子能够遍历区间.然后利用事先筛出的素数求出质因子,之后因子个数为全部(质因子的 ...

  2. 牛客:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数(数论+贪心)

    https://ac.nowcoder.com/acm/contest/907/B t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 分析: 根据约数和定理:对于一个大于1正整数 ...

  3. LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)

    #6278. 数列分块入门 2 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 6   题目描述 给出 ...

  4. UVA - 294 Divisors (约数)(数论)

    题意:输入两个整数L,U(1<=L<=U<=109,U-L<=10000),统计区间[L,U]的整数中哪一个的正约数最多.如果有多个,输出最小值. 分析: 1.求一个数的约数, ...

  5. UVa 294 - Divisors 解题报告 c语言实现 素数筛法

    1.题目大意: 输入两个整数L.H其中($1≤L≤H≤10^9,H−L≤10000$),统计[L,H]区间上正约数最多的那个数P(如有多个,取最小值)以及P的正约数的个数D. 2.原理: 对于任意的一 ...

  6. Uva 294 Divisors(唯一分解定理)

    题意:求区间内正约数最大的数. 原理:唯一分解定义(又称算术基本定理),定义如下: 任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积  ,这里  均为质数,其诸指数  是正整数.这样的分解称 ...

  7. UVA 294 - Divisors 因子个数

    Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an ...

  8. B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)

    题目链接:https://cn.vjudge.net/contest/284294#problem/B 题目大意:查询区间内有多少个不相同的数. 具体思路:主席树的做法,主席树的基础做法是查询区间第k ...

  9. #6278. 数列分块入门 2(询问区间内小于某个值 xx 的元素个数)

    题目链接:https://loj.ac/problem/6278 题目大意:中文题目 具体思路:数列分块模板题,对于更新的时候,我们通过一个辅助数组来进行,对于原始的数组,我们只是用来加减,然后这个辅 ...

随机推荐

  1. [Uva1642]魔法Gcd(数论)

    Description 给定n个数,某个连续区间[L,R]的收益为\(gcd(A_l,A_{l+1},A_{l+2}...A_r)*(r-l+1)\), 求收益最大的区间的收益值 \(1 \leq n ...

  2. LeetCode刷题感想

    断断续续用了半年的时间把LeetCode刷完了,之前复习了数据结构与算法.将刷题与复习数据结构结合起来会更有效果.总之不是为了刷题而刷题,而是为了巩固和补充一部分知识. LeetCode真的是一个很好 ...

  3. 【转载】2015年8月编程语言排行榜:Java遥遥领先

    Java以4.5%的差距遥遥领先于第二名,回顾以前Java有这样的成绩还是在2008年.Java version 8的成功主要是因为函数式编程习语的添加.Java出现下滑是在2010年 Oracle收 ...

  4. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  5. c语言入门-01

    当我们学c语言我们学些什么. [1]编译机制 当我们写好c的代码,生产了程序,这中间到底做了些什么? 这个就是c语言的编译过程 我们分别来解析这上面的过程. 我们写出我们第一个c程序. #includ ...

  6. kvm配置虚拟机[待整理]

    working note 4-12-2016 1,利用libvirt图形虚拟机管理工具virt-manager搭建虚拟机,通过存储池(storage pool )和卷(volume)存放虚拟机镜像(I ...

  7. Java中转发与重定向的区别

        转发与重定向的区别 转发是服务器行为,重定向是客户端行为 1.转发在服务器端完成的:重定向是在客户端完成的2.转发的速度快:重定向速度慢3.转发的是同一次请求:重定向是两次不同请求4.转发不会 ...

  8. sql server 韩文查询匹配失败

    在SQL Server 中查询韩文信息时,没有匹配到对应的信息,检查程序后发现字段类型是nvarchar类型的没有问题, 打开存储过程后找到问题了:原来是拼接后的查询语句存储在一个varchar变量中 ...

  9. Log4j官方文档翻译(九、输出到数据库)

    log4j提供了org.apache.log4j.JDBCAppender对象,可以把日志输出到特定的数据库. 常用的属性: bufferSize 设置buffer的大小,默认是1 driver 设置 ...

  10. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...