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

    Strategies 控制task的执行方式, 在2.0中增加了"free" Strategies, 可以允许每个host尽快的执行完一个play. 默认是Strategies是l ...

  2. 解决Mac Linux USB Loader“Couldn't get security scoped bookmarks”错误

    Mac Linux USB Loader出现 "Couldn't get security scoped bookmarks"错误时, 需要删除 ~/Library/Contain ...

  3. NYOJ-1057 寻找最大数(三)(贪心)

    寻找最大数(三) 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数. 求这个新的整数的 ...

  4. json_encode不编码中文字符的方式

    json_encode($array,JSON_UNESCAPED_UNICODE);

  5. 找轮转后的有序数组中第K小的数

    我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数. 此处假设数组中没有相同的数,原排列顺序是递增排列. 在轮转后的有序数组中查找最小数的算法 ...

  6. 关于指针要注意的地方还有尝试在codeblocks上建立项目

    1.字符串: char a[]="house"; char *b="house"; a[2]='r';可以   b[2]='r'不可以,因为这个指针变量指的是字 ...

  7. angular localStorage使用方法

    var YourCtrl = function($scope, localStorageService, ...) { // To add to local storage localStorageS ...

  8. 基础dp

    队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...

  9. HDU 4990 Reading comprehension

    快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...

  10. 一些Wifi破解姿势

    wlan0:无线网卡设备 BSSID/AP's MAC:目标路由器的mac地址 Client's MAC:连接到此wifi客户端的mac地址 ESSID:这个无线的名字 大致思路: 获取bssid和e ...