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 ...
随机推荐
- day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)
1. 内置函数 # ### 内置函数 # abs 绝对值函数 res = abs(-10) print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 奇进 ...
- Caused by: java.net.ConnectException: Connection timed out: connect
发生这种情况的原因是:连接的路径发生错误
- Day1-XDoj-1062
题目描述 一天,lw梦见自己在打dota,然而对面是一个加强过的卡尔!于是,他每次都被n个技能瞬间秒杀.愤怒的lw决定买BKB,来加强生存力. 由于加强过的卡尔是电脑操作的,他每次看见lw时,只会以1 ...
- 使用Go语言一段时间的感受
作者 openkk 2012-03-04 18:26:58 文/Windstorm 有一段时间没更新了.最近在忙一个 Server+Client 的项目,Client 是 Android 手机,大概也 ...
- bootstrap上下左右条纹边框悬停表格的使用
懒得找地址:https://jingyan.baidu.com/album/0bc808fc3d88941bd585b94c.html?picindex=1
- 题解 CF1131C 【Birthday】
CF大水题 题意:给你n个人,他们的身高是a[i],让你将这几个人排成一个环,使得他们两两之间身高差的和最小. 思路:简单到爆了,恶意评分上蓝.直接将那几个人排个序,然后按序左右放就行了,也就是说1号 ...
- NULL判斷符
Null 传导运算符 编程实务中,如果读取对象内部的某个属性,往往需要判断一下该对象是否存在.比如,要读取message.body.user.firstName,安全的写法是写成下面这样. const ...
- Jquery所有Dom操作汇总
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【HITB GSEC CTF 2017】1000levels
https://files.cnblogs.com/files/p4nda/498a3f10-8976-4733-8bdb-30d6f9d9fdad.gz #通过阅读天枢战队大佬们的wp调试的结果 首 ...
- ROS常用库(四)API学习之常用common_msgs(下)
一.前言 承接ROS常用库(三)API学习之常用common_msgs(上). 二.sensor_msgs 1.sensor_msgs / BatteryState.msg #电源状态 uint8 P ...