HDU2710-Max Factor-分解质因子
给出N个MAXN以内的不同的数,求出素因子最大的数。
使用朴素的方法分解素因子即可。时间复杂度为N*log(MAXN)
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = ;
int N,save[];
int prime[maxn]; void init()
{
for(int i=;i<=maxn;i++)
{
if(!prime[i]) prime[++prime[]] = i;
for(int j=;j<=prime[]&&prime[j]<=maxn/i;j++)
{
prime[prime[j]*i] = ;
if(i%prime[j]==) break;
}
}
} int getFactors(int x)
{
int tmp = x;
int ans = ;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
if(tmp%prime[i] == )
{
ans = max(ans,prime[i]);
while(tmp%prime[i] == )
{
tmp/=prime[i];
}
}
}
if(tmp != )
{
ans = max(ans,tmp);
}
return ans;
} int main()
{
init();
int ans,mxp;
while(~scanf("%d",&N))
{
ans=;mxp=;
for(int i=;i<N;i++)
{
scanf("%d",&save[i]);
int p = getFactors(save[i]);
if(p > mxp) {ans = save[i];mxp = p;}
}
if(ans == ) ans = ;
printf("%d\n",ans);
}
}
HDU2710-Max Factor-分解质因子的更多相关文章
- HDU 4497 GCD and LCM(分解质因子+排列组合)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...
- UVA 10780 Again Prime? No Time. 分解质因子
The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...
- hdu6237 分解质因子
题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...
- NYOJ-476谁是英雄,分解质因子求约数个数!
谁是英雄 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 十个数学家(编号0-9)乘气球飞行在太平洋上空.当横越赤道时,他们决定庆祝一下这一壮举.于是他们开了一瓶香槟.不 ...
- Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)
题目链接 题目大意 给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件: 1. a<x<c,b<y<d 2. (x*y)%(a ...
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4135 Co-prime (容斥+分解质因子)
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...
- Minimum Sum LCM UVA - 10791(分解质因子)
对于一个数n 设它有两个不是互质的因子a和b 即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...
- BNU 13259.Story of Tomisu Ghost 分解质因子
Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost ...
随机推荐
- Docker for .Net Developers(part1:Docker基本概念介绍)
一.什么是Docker 目前,.Net 社区中很可能会用到的两个词是“微服务”和“Docker”. 这两个主题都非常引人注目,并为开发人员和架构师带来兴奋之情. 在这个新系列的博客文章中,我把自己最近 ...
- python 获取gearbest地址库代码
import requests import json # 用来去掉多余的字符,并格式化 def geshihua(str): s = None if "/**/_get_country(& ...
- 1060E Sergey and Subway(思维题,dfs)
题意:给出一颗树,现在,给哪些距离为2的点对,加上一条边,问所有点对的距离和 题解:如果没有加入新的边,距离和就会等于每条边的贡献,由于是树,我们用点来代表点上面的边,对于每条边,它的贡献将是(子树大 ...
- C语言之运算符、表达式和语句
#include<stdio.h> #define ADJUST 7.31 int main(void) { const double SCALE = 0.333; double shoe ...
- java问题
Collection 和 Collections的区别? Collection是集合类的上级接口,继承与他的接口主要有Set 和List. Collections是针对集合类的一个帮助类,他提供一系列 ...
- 简约时尚商城wordpress主题-storefront
wordpress主题:简约时尚商城主题-storefront 简简单的商城模板,挺适合一些懒人所用.后天功能也挺不错,希望大家喜欢. WooCommerce 集成 商城是基为用 WooCommerc ...
- Servlet处理GET和POST请求
doGet() . doPost().service()方法 doGet()表示,当客户端是使用get方式请求该servlet时,那么就会触发执行doGet()方法中的代码. doPost()表示,当 ...
- Jenkins deploy war to tomcat over https
ssl - HTTPS login with Spring Security redirects to HTTP - Stack Overflow https://stackoverflow.com/ ...
- swagger 指定字段不显示到文档里
Swagger UI 隐藏指定接口类或方法 - 宁静致远 - CSDN博客https://blog.csdn.net/lqh4188/article/details/53538201 swagger ...
- Zabbix appliance One Stop
Download Zabbix appliancehttps://www.zabbix.com/download_appliance