Uva 294 Divisors(唯一分解定理)
2024-08-31 12:35:35
题意:求区间内正约数最大的数。
原理:唯一分解定义(又称算术基本定理),定义如下:
任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积 ,这里 均为质数,其诸指数 是正整数。这样的分解称为
的标准分解式。(取自百度百科)
根据原理,正约数数量 = (1+a1)(1+a2)..(1+an)
因此我们需要先求出所有素数,进而求出a1,a2,..an的大小。
原题给的数字范围是1<=L<=U<=10^9,假如要全部算一遍需要很长时间。那么可能最大的正约数是多少呢?
回想我们所用的判断素数的算法(第五行到第十二行),最大可能的正约数不会超过根号n。求得n大约是31622。
然后我们就可以把1<=n<=31622内的素数打表来加速我们的计算。
完整代码如下:
#include <bits/stdc++.h>
using namespace std;
vector<int> prime;
//
int is_prime(int n)
{
int k = floor(sqrt(n)+0.5);
for(int i = ; i <= k; i++){
if(n%i==)return ;
}
return ;
}
int divinum(int n){
int sum = ;
unsigned k = ;
while(n>&&k<prime.size()){
int t = ;
while(n%prime[k]==){
n/=prime[k];
t++;
}
k++;
sum*=t;
}
return sum;
}
int main(){
int n;
for(int i = ; i <= ; i++){
if(is_prime(i))
prime.push_back(i);
}
cin >> n;
while(n--){
int L, U;
cin >> L >> U;
int max_factor = ;
max_factor = ;
int max_num = ;
for(int i = L; i <= U; i++){
int k = divinum(i);
if(k > max_factor){
max_factor = k;
max_num =i;
}
}
printf("Between %d and %d, %d has a maximum of %d divisors.\n",L,U,max_num,max_factor);
}
return ;
}
最新文章
- Oracle PL/SQL随堂笔记总结
- TypeScript的4种编译方式
- Python 开发轻量级爬虫06
- RPi 2B Raspbian SD卡内部架构
- 在 slua 中使用更新的面向对象方案
- 更改linux系统时间
- 哥德尔,图灵和康托尔 part 2 停机问题
- UE4 C++ 跳转网页
- python迭代器以及itertools模块
- 《java入门第一季》之面向对象(一个易错面试题)
- Net包管理NuGet(1)nuget的使用方法
- Luogu P1967 货车运输
- jquery弹窗时禁止body滚动条滚动
- u-boot移植(二)---修改前工作:代码流程分析1
- php单例模式的实例
- org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.*.Paper.totalTime
- [Spring] 关联类和bean | autowire=byName|byType
- 三种css样式表及其优先级
- golang语言并发与并行——goroutine和channel的详细理解(一) 转发自https://blog.csdn.net/skh2015java/article/details/60330785
- 偏流角为什么是arcsin(w/V)
热门文章
- vue2高仿饿了么app
- 分组函数group by和Oracle中分析函数partition by的用法以及区别
- 【学时总结&;模板时间】◆学时&#183;10 &; 模板&#183;3◆ AC自动机
- Linux的开山篇
- oracle12c管理作业资源的一种方式
- LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)
- 【解决】MongoDB 线上业务处理,数据去重脚本实现
- Linux给当前用户指定目录授权命令
- 如何在linux系统内用openssl 生成 过期的证书
- C语言实例解析精粹学习笔记——30