UVA - 294 Divisors (约数)(数论)
题意:输入两个整数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 (约数)(数论)的更多相关文章
- UVA - 294 Divisors【数论/区间内约数最多的数的约数个数】
Mathematicians love all sorts of odd properties of numbers. For instance, they consider to be an int ...
- 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 ...
- UVA 294 294 - Divisors (数论)
UVA 294 - Divisors 题目链接 题意:求一个区间内,因子最多的数字. 思路:因为区间保证最多1W个数字,因子能够遍历区间.然后利用事先筛出的素数求出质因子,之后因子个数为全部(质因子的 ...
- The number of divisors(约数) about Humble Numbers[HDU1492]
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- The number of divisors(约数) about Humble Numbers
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- HDU1492/The number of divisors(约数) about Humble Numbers
题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- HDUOJ---The number of divisors(约数) about Humble Numbers
The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
随机推荐
- main.js index.html与app.vue三者关系详解
main.js index.html与app.vue三者关系详解 2019年01月23日 11:12:15 Pecodo 阅读数 186 main.js与index.html是nodejs的项目启 ...
- IDEA 单行注释与代码对齐
效果 修改步骤 Settings -> Editor -> Code Style (1)修改.java文件的注释 comment 评论.注释.意见. (2)修改.html文件的注释 ( ...
- IOS switch-case知多少
1. switch参数类型 switch参数类型要求是integer type,准确来讲,是可以转换成integer的类型, 这包括所有的C基本数据类型((signed/unsigned)char, ...
- 拖放获取文件信息的bat代码
参考:岁月如歌-通过拖曳获取文件信息的bat代码 拖放获取文件信息的bat代码 使用命令行配合7z解压文件时由于每次解压的文件不同,因此搜索了一下拖放识别文件信息的方法,以此方式来减轻工作量 获取文件 ...
- 2.Jsoup
public static void main(String[] args) { //爬取最大资源网上的数据 //用CSS选择器 try { Document doc = Jsoup.parse(ne ...
- Heap(堆)的基础知识入门
堆 逻辑结构: 1 / \ 1 3 / \ / \ 4 5 6 null 物理结构; 1.首先堆是一个完全二叉查找书(Co ...
- 初学微信小程序——配置问题(1)
一.注册: 微信小程序账号注册:登录https://mp.weixin.qq.com 点击“立即注册”->”小程序” 注册完成后,下载微信小程序开发者工具: 依次点击:“首页”->“文档 ...
- Andorid 搭建 Linux服务器(一)
00.搭建环境 电脑系统:MacOS下Win7虚拟机 手机型号:红米Note5A 手机系统:MIUI10开发版 软件: SuperSU --通过recovery刷入,管理ROOT权限 Bus ...
- PL/SQL 找到某列都为空的列名
DECLARE CURSOR temp IS SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME=Upper('xxx'); v_num ...
- CSS样式表——样式2
样式 5)边界边框 margin:0px; //外边距为0 margin:10px 0px 0px 10px; ...