题意转化一下就是寻找一个数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 ;
}

最新文章

  1. R 单变量重命名与删除
  2. Libevent初探
  3. ubuntu16.04中将python3设置为默认
  4. Web前端开发基础 第二天(各类标签)
  5. oc-26-动态类型检测
  6. C++问题-无法打开某个自定义源文件
  7. C#微信公众号开发 -- (四)获取API调用所需的全局唯一票据access_token
  8. uva 508 Morse Mismatches
  9. tolua 有些功能可以用(经过测试)
  10. php系统无法上传图片问题
  11. plsql developer 中文乱码(???)解决办法
  12. 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)
  13. Android24以上拍照代码
  14. 【图文详细教程】maven3安装配置+eclipse离线安装maven3插件《《唯一成功的教程~~~2018-01-09》》
  15. Vue-vue-cli初始化项目
  16. Array map()方法
  17. 在Windows上开发PHP扩展模块
  18. 【SpringBoot+Mybatis+thymeleaf报错】Error resolving template &quot;XXX&quot;, template might not exist or might not be accessible by any of the configured
  19. Junit打包测试
  20. java拾遗5----Java操作Mongo入门

热门文章

  1. 物料事务处理接口表 MTL_TRANSACTIONS_INTERFACE 账户别名使用 及 提示无效的分配账户字段
  2. fork()子进程与waitpid()
  3. Android编程获取手机的IMEI
  4. 【.NET】字符串处理
  5. Faster-R-CNN编译使用及相应问题解决
  6. 下载、安装jdk8(Windows下)并配置变量环境
  7. How to solve java.net.SocketTimeoutException:60000millis problem in HDFS
  8. 在家用机上搭建 Git https 服务器
  9. 第六十三节,html表格元素
  10. php获取本周周一、周日时间,上周周一、周日时间,本月第一天,本月最后一天,上个月第一天,最后一天时间