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. Redis实现之事件

    事件 Redis服务器是一个事件驱动程序,服务器需要处理以下两类事情: 文件事件(file event):Redis服务器通过套接字与客户端(或者其他Redis服务器)进行连接,而文件事件就是服务器对 ...

  2. 小米r3g旧版开发版固件,安装opkg

    1.开启ssh 1.1.刷入固件 在路由器更新界面,刷入 miwifi_r3g_firmware_c2175_2.25.122.bin 固件 下载地址: http://bigota.miwifi.co ...

  3. Android TV 开发(4)

    本文来自网易云社区 作者:孙有军 最后我们再来看看好友界面,改界面本地是没有xml的,因此我们直接来看看代码: 这里将使用到数据bean,与数据源的代码也贴出来如下: public class Con ...

  4. Python对文本文件的简单操作(一)

    工作背景 性能测试工程师,主要测试工具--loadrunner,主要是接口测试. 实现功能 loadrunner对报文格式的转换存在问题,部分报文无法转换,故使用Python编写脚本自动将soap协议 ...

  5. mvcs项目搭建

    项目结构 下载链接:http://pan.baidu.com/s/1hsGtShA

  6. CMake 使用笔记

    记录 CMake 相关知识. Prelude:读文档一定要有耐心! 问题一 CLion: CMakeLists.txt 中 set(CMAKE_CXX_FLAGS -Wall) 不起作用 Soluti ...

  7. [CQOI2014][bzoj3507] 通配符匹配 [字符串hash+dp]

    题面 传送门 思路 0x01 KMP 一个非常显然而优秀的想法:把模板串按照'*'分段,然后对于每一段求$next$,'?'就当成可以对于任意字符匹配就行了 对于每个文本串,从前往后找第一个可以匹配的 ...

  8. 个人环境搭建——搭建JDK环境

    搭建JDK环境 开始之初先提醒两点: ①java是在bash环境下面的,虽然我也在.cshrc下面添加了环境变量,好像有点问题,需要继续改进: ②查看linux版本信息命令:cat /etc/issu ...

  9. codechef May Challenge 2016 FORESTGA: Forest Gathering 二分

    Description All submissions for this problem are available. Read problems statements in Mandarin Chi ...

  10. react自定义组件属性类型检测

    react当中的props-type用来检测传入组件当中的数据是否符合组件的要求,但是之前的只是能做些简单常规的判断,如果需要做复杂的判断,就需要使用到自定义函数来做类型检测了. 下面是官网的例子 c ...