题意转化一下就是寻找一个数P,要求P质因素分解完后,质因素没有重复,还要保证abs(P*P-x)最小。

暴力,在sqrt(x)附近向下向上分别枚举一下。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int S=; LL mult_mod(LL a,LL b,LL c)
{
a%=c; b%=c; LL ret=;
while(b)
{
if(b&){ret+=a;ret%=c;} a<<=;
if(a>=c)a%=c; b>>=;
}
return ret;
} LL pow_mod(LL x,LL n,LL mod)
{
if(n==)return x%mod;
x%=mod; LL tmp=x,ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod); n>>=;
}
return ret;
} bool check(LL a,LL n,LL x,LL t)
{
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;
last=ret;
}
if(ret!=) return true;
return false;
} bool Miller_Rabin(LL n)
{
if(n<)return false;
if(n==)return true;
if((n&)==) return false;
LL x=n-,t=;
while((x&)==){x>>=;t++;}
for(int i=;i<S;i++)
{
LL a=rand()%(n-)+;
if(check(a,n,x,t)) return false;
}
return true;
} LL factor[];
int tol; LL gcd(LL a,LL b)
{
if(a==)return ;
if(a<) return gcd(-a,b);
while(b) { LL t=a%b; a=b; b=t; }
return a;
} LL Pollard_rho(LL x,LL c)
{
LL i=,k=,x0=rand()%x,y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
} void findfac(LL n)
{
if(Miller_Rabin(n)) { factor[tol++]=n; return; }
LL p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p); findfac(n/p);
} int T;
LL x,ans; int main()
{
srand(time(NULL)); scanf("%d",&T);
while(T--)
{
scanf("%lld",&x);
if(x==) {printf("3\n"); continue;}
if(x==) {printf("2\n"); continue;}
if(x==) {printf("1\n"); continue;} LL n=(LL)sqrt(1.0*x);
ans=x;
for(LL i=n;i>=;i--)
{
tol=; findfac(i); sort(factor,factor+tol);
bool fail=; for(int j=;j<tol-;j++) if(factor[j]==factor[j+]) fail=;
if(fail==) continue;
else { ans=abs(x-i*i); break;}
} for(LL i=n+;;i++)
{
tol=; findfac(i); sort(factor,factor+tol);
bool fail=; for(int j=;j<tol-;j++) if(factor[j]==factor[j+]) fail=;
if(fail==) continue;
else { ans=min(ans,abs(x-i*i)); break;}
}
printf("%lld\n",ans); }
return ;
}

HDU 5778 abs的更多相关文章

  1. HDU 5778 abs (枚举)

    abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...

  2. HDU 5778 abs 数学

    http://acm.hdu.edu.cn/showproblem.php?pid=5778 这题的意思就是找离x最近的一个数y,且y是一个完全平方数,还是所有质因子都只能出现两次的完全平方数 一开始 ...

  3. HDU 5778 abs (素数,暴力)

    题意:给定一个数x,求正整数y≥2y\geq 2y≥2,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 析:由于y质因数分解式中每个质因数均出现2次,那么 ...

  4. HDU 5778 abs (暴力枚举)

    abs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem De ...

  5. HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力

    分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...

  6. 【HDU5778】abs(数学)

    BUPT2017 wintertraining(16) #4 C HDU - 5778 题意 给定x,找出使|y-x|最小,且每个质因子都出现两次的y(\(y\le 2\))50组测试数据,\(1\l ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. *HDU 1709 母函数

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 4547(LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 思路:这题的本质还是LCA问题,但是需要注意的地方有: 1.如果Q中u,v的lca为u,那么只需 ...

随机推荐

  1. Multidimensional Array And an Array of Arrays

    One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform. T ...

  2. 2015 asia xian regional F Color (容斥 + 组合数学)

    2015 asia xian regional F Color (容斥 + 组合数学) 题目链接http://codeforces.com/gym/100548/attachments Descrip ...

  3. checkbox:获取所有已选中的值

    /**获取选中的checkbox值*/ function getChecked(){ var ids = ""; $("input:checkbox[name='id'] ...

  4. java 中递归的实现 以及利用递归方法实现汉诺塔

    今天说下java语言中比较常见的一种方法,递归方法. 递归的定义 简单来说递归的方法就是"自己调用自己",通过递归方法往往可以将一个大问题简单化,最终压缩到一个易于处理的程度.对于 ...

  5. Django开发环境配置

    Eclipse   首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官网下载JDK安装:ht ...

  6. B/S、C/S区别

    [B/S.C/S C/S (Client/Server客户端服务器) B/S (Brower/Server浏览器服务器)  区别 1.硬件环境不同: C/S 一般建立在专用的网络上, 小范围里的网络环 ...

  7. 超链接解决头部fixed问题

    ///////////超链接解决头部fixed问题 $('a[href*=#]').click(function () { var top1 = $(".header").heig ...

  8. openstack私有云布署实践【2 安装前的服务器基本环境准备】

    服务器物理机都安装centos7.2 1511版本 , 此次采用的分区方式全是自动XFS格式LVM,在装系统时就将所有本地raid5硬盘都加入LVM全用了.默认/home目录有着最大的硬盘空间 并且我 ...

  9. iOS开发Embedded dylibs/frameworks are only supported on iOS 8.0 and later for architecture armv7的解决方法

    全局搜索IPHONEOS_DEPLOYMENT_TARGE改为更小的版本

  10. Ambari安装组件出错

    Caught an exception while executing custom service command: <class 'ambari_agent.AgentException.A ...