UVA - 294 Divisors【数论/区间内约数最多的数的约数个数】
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【数论/区间内约数最多的数的约数个数】的更多相关文章
- UVA 294 294 - Divisors (数论)
UVA 294 - Divisors 题目链接 题意:求一个区间内,因子最多的数字. 思路:因为区间保证最多1W个数字,因子能够遍历区间.然后利用事先筛出的素数求出质因子,之后因子个数为全部(质因子的 ...
- 牛客:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数(数论+贪心)
https://ac.nowcoder.com/acm/contest/907/B t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 分析: 根据约数和定理:对于一个大于1正整数 ...
- LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)
#6278. 数列分块入门 2 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 6 题目描述 给出 ...
- UVA - 294 Divisors (约数)(数论)
题意:输入两个整数L,U(1<=L<=U<=109,U-L<=10000),统计区间[L,U]的整数中哪一个的正约数最多.如果有多个,输出最小值. 分析: 1.求一个数的约数, ...
- UVa 294 - Divisors 解题报告 c语言实现 素数筛法
1.题目大意: 输入两个整数L.H其中($1≤L≤H≤10^9,H−L≤10000$),统计[L,H]区间上正约数最多的那个数P(如有多个,取最小值)以及P的正约数的个数D. 2.原理: 对于任意的一 ...
- Uva 294 Divisors(唯一分解定理)
题意:求区间内正约数最大的数. 原理:唯一分解定义(又称算术基本定理),定义如下: 任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积 ,这里 均为质数,其诸指数 是正整数.这样的分解称 ...
- UVA 294 - Divisors 因子个数
Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an ...
- B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)
题目链接:https://cn.vjudge.net/contest/284294#problem/B 题目大意:查询区间内有多少个不相同的数. 具体思路:主席树的做法,主席树的基础做法是查询区间第k ...
- #6278. 数列分块入门 2(询问区间内小于某个值 xx 的元素个数)
题目链接:https://loj.ac/problem/6278 题目大意:中文题目 具体思路:数列分块模板题,对于更新的时候,我们通过一个辅助数组来进行,对于原始的数组,我们只是用来加减,然后这个辅 ...
随机推荐
- hihocoder1015 kmp算法
#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- HDU - 6514 Monitor(二维差分)
题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...
- 笔记-算法-KMP算法
笔记-算法-KMP算法 1. KMP算法 KMP算法是一种改进的字符串匹配算法,KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一 ...
- MySQL和PostgreSQL比较
1.MySQL相对来说比较年轻,首度出现在1994年.它声称自己是最流行的开源数据库.MySQL就是LAMP(用于Web开发的软件包,包括 Linux.Apache及Perl/PHP/Python)中 ...
- MySQL基础2-创建表和主键约束
1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...
- CLOUDSTACK FOR HYPER-V
原文地址:http://zhu.vn/archives/1040 我这里是内网测试环境,宿主机为Server 2012R2 ,虚拟化技术为HYPER-V,域环境来的(不是域环境玩不了). 先给宿主机安 ...
- load_file()与into outfile函数详解
load_file()函数的使用: 1.使用条件 ①有读取文件的权限 r and (select count(*) from mysql.user)>0 如果返回正常则说明有权限,反之没有 ②文 ...
- python拼接
拼接: name=zhuhuan age=23 salary=333 info=''' ----- info of %s----- age:%s name:%s salary:%s %(name,ag ...
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- 与Python的初次见面
一.Python的介绍 python的创始人为吉多.范罗苏姆.1989年的圣诞期间,吉多.范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 二.Python是 ...