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

分析:

1、求一个数的约数,相当于分解质因子。

2、例如60 = 2 * 2 * 3 * 5。对于2来说,可选0个2,1个2,2个2,有3种情况,同理对于3,有2种情况,对于5,有2种情况,所以3 * 2 * 2则为60的约数个数。

3、L到U扫一遍,取最大值即可。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
if(fabs(a - b) < eps) return 0;
return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 35000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int vis[MAXN];
vector<int> prime;
void init(){
for(int i = 2; i < MAXN; ++i){
if(!vis[i]){
prime.push_back(i);
for(int j = 2 * i; j < MAXN; j += i){
vis[j] = 1;
}
}
}
}
int cal(int n){
int ans = 1;
int len = prime.size();
for(int i = 0; i < len; ++i){
if(prime[i] > n) break;
if(n % prime[i]) continue;
int cnt = 1;
while(n % prime[i] == 0){
++cnt;
n /= prime[i];
}
ans *= cnt;
}
return ans;
}
int main(){
init();
int T;
scanf("%d", &T);
while(T--){
int l, r;
scanf("%d%d", &l, &r);
int ans = 0;
int id;
for(int i = l; i <= r; ++i){
int tmp = cal(i);
if(tmp > ans){
ans = tmp;
id = i;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, r, id, ans);
}
return 0;
}

  

UVA - 294 Divisors (约数)(数论)的更多相关文章

  1. UVA - 294 Divisors【数论/区间内约数最多的数的约数个数】

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

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

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

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

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

  4. UVA 294 - Divisors 因子个数

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

  5. UVA 294 294 - Divisors (数论)

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

  6. The number of divisors(约数) about Humble Numbers[HDU1492]

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  7. The number of divisors(约数) about Humble Numbers

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  8. HDU1492/The number of divisors(约数) about Humble Numbers

    题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...

  9. HDUOJ---The number of divisors(约数) about Humble Numbers

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. 在linux环境中配置solr

    第一步:安装linux.jdk.tomcat. 第二步:把solr的压缩包上传到服务器.并解压.我的solr压缩包是解压在/usr/local/solr/包下的 系统默认是没有solr包的需要自己创建 ...

  2. Raspbian设置静态ip

    Raspbian static ip 最近入手了树莓派4b,并更具官方教程安装了Raspbian.由于直接通过wifi连接,每次ip跳来跳去很不方便,于是便想着设置静态ip. 由于Raspbian本身 ...

  3. JAVA地址栏重写很详细

    这几天蛋疼.看看别人url重写是怎么搞的..1.解释下什么事url重写,以及它的优缺点: URL重写,其实就是把带一大堆参数的url,变成一个看上去很规矩的url.例:/viewthread.jsp? ...

  4. CentOS7 环境下 在Hadoop集群安装Hive

    1.下载Hive的tar.gz包:http://mirror.bit.edu.cn/apache/hive/ 2.放入CentOS 7 系统中并解压:tar -zxvf apache-hive-2.3 ...

  5. Lua 完美打印数据 (例子)

    例子1 : ableprint = function(data,cstring,deepIndex) --第二个参数可以为空,第三个参数不要手动添加,它是用来进行打印深度控制的. if data == ...

  6. I2S 总线学习:2-I2S驱动WM8978

    背景 为了了解I2S总线所对应的硬件设计,下文转载了<STM32:I2S驱动WM8978>. 以加深对I2S总线的了解. 正文 最近项目中使用STM32F4驱动音频IC:WM8978. 由 ...

  7. arm linux 移植 rsync

    背景: 在产品开发中可以使用rsync进行大文件的拷贝,断点续传. host平台 :Ubuntu 16.04 arm平台 : 3531d   rsync   :3.1.3 arm-gcc :4.9.4 ...

  8. P1087 有多少不同的值

    P1087 有多少不同的值 转跳点:

  9. HiBench成长笔记——(9) Centos安装Maven

    Maven的下载地址是:http://maven.apache.org/download.cgi 安装Maven非常简单,只需要将下载的压缩文件解压就可以了. cd /home/cf/app wget ...

  10. Docker退出容器不关闭容器的方法

    进入docker容器后如果退出容器,容器就会变成Exited的状态,那么如何退出容器让容器不关闭呢? 如果要正常退出不关闭容器,请按Ctrl+P+Q进行退出容器,这一点很重要,请牢记! 以下示例为退出 ...