HDU 5778 abs
题意转化一下就是寻找一个数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的更多相关文章
- HDU 5778 abs (枚举)
abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...
- HDU 5778 abs 数学
http://acm.hdu.edu.cn/showproblem.php?pid=5778 这题的意思就是找离x最近的一个数y,且y是一个完全平方数,还是所有质因子都只能出现两次的完全平方数 一开始 ...
- HDU 5778 abs (素数,暴力)
题意:给定一个数x,求正整数y≥2y\geq 2y≥2,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 析:由于y质因数分解式中每个质因数均出现2次,那么 ...
- HDU 5778 abs (暴力枚举)
abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem De ...
- HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...
- 【HDU5778】abs(数学)
BUPT2017 wintertraining(16) #4 C HDU - 5778 题意 给定x,找出使|y-x|最小,且每个质因子都出现两次的y(\(y\le 2\))50组测试数据,\(1\l ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- *HDU 1709 母函数
The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4547(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 思路:这题的本质还是LCA问题,但是需要注意的地方有: 1.如果Q中u,v的lca为u,那么只需 ...
随机推荐
- removeObjectAtIndex
CGFloat lableW = (baseViewWidth - 2)/3;// dcj20150724,减2是为了解决字体模糊的问题,因为设置了边框. 原因是下面引起的 titleview.lay ...
- 浏览器exp使用经验
0x00背景 windows平台下,浏览器安全是绕不过的话题,其涉及的安全问题涵盖二进制和web,攻击场景也非常多样化: 用户点击攻击者的恶意URL链接被感染恶意代码 访问恶意站点被绕过同源策略窃取c ...
- JAVA MONGODB group查询的UTC时间问题
BasicDBList dateList = new BasicDBList(); dateList.add("$t"); dateList.add(28800000); DBOb ...
- php常用数学函数
php常用数学函数1. bcadd 任意精度数的相加2. bcsub 任意精度数的减法3. bcmul 乘法, bcdiv除法 4. bcmod 取余数. (比%功能更强大)5. bcpow 幂函数运 ...
- OpenCV FileStorage 使用记录
FileStorage OpenCV 中的 FileStorage 类能够读写硬盘中的.xml和.yaml文件,这里我们只讨论对 .xml 的以下几种操作: 写入(FileStorage::WRITE ...
- 理解委托(delegate)及为什么要使用委托
理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...
- pdf 电子书分享
http://yunpan.cn/cLgXntGmIas7A 访问密码 7d04 来自为知笔记(Wiz)
- angular2与VS2015 asp.net core集成使用
首先,需要在VS2015环境下更新到update2,并安装asp.net core环境,并安装Node.js插件. 新建asp.net core项目: 此时,需要先新建npm配置文件,如图: 并定义n ...
- android app 集成 信鸽推送
推送其实挺中意小米推送的,并经用户群占比还是比较大的,奈何拗不过php后端哥们的选型,就只好用信鸽推送了,期间接入过程中也是遇到不少问题,所以记录下来,以后如果还是用信鸽推送的话,估计看看以前的博客, ...
- 《JS权威指南学习总结--8.8.4 记忆函数》
内容要点: 可以将上次的计算结果缓存起来.在函数式编程当中,这中缓存技巧叫做 "记忆". 需要注意的是,记忆只是一种编程技巧,本质上是牺牲算法的空间复杂度以换取更优的时间复杂度 ...