HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小
大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考
CDOJ:无平方因子数 http://acm.uestc.edu.cn/#/problem/show/618
这个题和CDOJ的题虽然不一样,但是可以从CDOJ发现这种数是很多的
官方题解:官方题解说这个无平方因子的枚举量在logn级别,可见非常小
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 1e5+;
bool v[N];
int prime[N>>],cnt;
void getprime(){
for(int i=;i*i<=N-;++i)
if(!v[i])
for(int j=i*i;j<=N-;j+=i)
v[j]=;
for(int i=;i<=N-;++i)
if(!v[i])prime[++cnt]=i;
}
LL fun(LL x,LL y){
if(x>y)swap(x,y);
return y-x;
}
int main(){
int T;
getprime();
scanf("%d",&T);
while(T--){
LL n;scanf("%I64d",&n);
LL x=(LL)(sqrt(1.0*n)+0.5),l=-,r=-;
for(int k=x;k>=;--k){
bool flag=false;
for(int i=;i<=cnt&&1ll*prime[i]*prime[i]<=k;++i){
if(k%(1ll*prime[i]*prime[i])==){flag=true;break;}
}
if(!flag){l=k;break;}
}
for(int k=x+;;++k){
bool flag=false;
for(int i=;i<=cnt&&1ll*prime[i]*prime[i]<=k;++i){
if(k%(1ll*prime[i]*prime[i])==){flag=true;break;} }if(!flag){r=k;break;}
}
LL ret;
if(l==-)ret=fun(r*r,n);
else ret=min(fun(n,l*l),fun(r*r,n));
printf("%I64d\n",ret);
}
return ;
}
HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力的更多相关文章
- HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题
分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- BestCoder Round #85 hdu5778 abs(素数筛+暴力)
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- HDU 5778 abs (素数,暴力)
题意:给定一个数x,求正整数y≥2y\geq 2y≥2,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 析:由于y质因数分解式中每个质因数均出现2次,那么 ...
- HDU 5778 abs 数学
http://acm.hdu.edu.cn/showproblem.php?pid=5778 这题的意思就是找离x最近的一个数y,且y是一个完全平方数,还是所有质因子都只能出现两次的完全平方数 一开始 ...
- HDU 5778 abs (枚举)
abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...
- BestCoder Round #85(ZOJ1569尚未验证)
A题 子序列和啊,就要想到前缀和的差.这个转换一定要!记着!那么i到j的一段子序列和Sij%m == 0就等价于(Sj-Si-1)%m == 0 了,那么什么意思呢?就是如果有两段前缀和%m的模是一 ...
- BestCoder Round #85
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
随机推荐
- POJ2299Ultra-QuickSort
http://poj.org/problem?id=2299 题意 : 排序,求排序次数,本来以为用冒泡可以搞定,事实上,那么大的数据以及一个TLE告诉我,会超时......... 思路 : 问了一下 ...
- Ubuntu环境下手动配置ant
配置ant 1. 下载ant(http://ant.apache.org/bindownload.cgi) 例如我下载的是:apache-ant-1.9.4-bin.tar.gz 解压ant,将文件夹 ...
- hibernate的简单学习(第一天)
sql脚本: -- Create table drop table T_PERSON; create table T_PERSON ( id ) PRIMARY KEY, name ), passwo ...
- *[hackerrank]Sam and sub-strings
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53 ...
- Spring与Hibernate整合
Spring与Struts2整合的目的: 让Spring管理Action Spring整合Hinernate的目的: --管理SessionFactory(单例的),数据源 --声明式事务管理 1.首 ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- mongodb group分组
先插入测试数据: for(var i=1; i<20; i++){ var num=i%6; db.test.insert({_id:i,name:"user_&quo ...
- HTML CSS3 手风琴菜单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...
- python3.4安装suds
使用suds访问webservice十分方便 python3.x安装suds会报错“No module named client” 在stackoverflow上找到了替代方法,安装suds-jurk ...
- 自定义View(6)paint设置图图层重叠时的显示方式,包含清空canvas
Paint.setXfermode 这个函数设置两个图层相交时的模式 在已有的图层上绘图将会在其上面添加一层新的图层. 如果新的图层是完全不透明的,那么它将完全遮挡住下面的图层,而setXfermod ...