http://acm.hdu.edu.cn/showproblem.php?pid=5778

思路:只有平方质因子的数,也就是这题所说的   y的质因数分解式中每个质因数均恰好出现2次  满足条件的数很幂集

因此枚举sqrt(x),前后判断一下sqrt(x)的质因子就可以

可以不判断是不是素数

注意x<4的情况

 // #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define pi acos(-1.0)
const int N = + ;
#define inf 0x7fffffff
typedef long long LL; void fre() { freopen("in.txt","r",stdin);} bool isPrime[N];
LL primeList[N],primeCount = ; LL Fast_power(LL n,LL k,LL mod) {
if(k == ) return ;
LL temp = Fast_power(n,k / ,mod);
temp = (temp * temp) % mod;
if(k % == ) temp = (temp * (n % mod)) % mod;
return temp;
} void Eular_Sieve(LL n) {
memset(isPrime,true,sizeof(isPrime));
isPrime[] = false;
isPrime[] = false;
for(int i = ; i <= n; i ++) {
if(isPrime[i]) {
primeCount ++;
primeList[primeCount] = i;
}
for(int j = ; j <= primeCount; j ++) {
if(i * primeList[j] > n) break;
isPrime[i * primeList[j]] = false;
if(!(i % primeList[j])) break;
}
}
} int Mr[] = {, , , , , , , , , , , };
bool Miller_Rabin(LL n) {
if(n == ) return true;
else if(n < ) return false;
if(n % == ) return false;
LL u = n - ;
while(u % == ) u = u / ;
int tempu = u;
for(int i = ; i < ; i ++) {
if(Mr[i] >= n) break;
u = tempu;
LL x = Fast_power(Mr[i],u,n);
while(u < n) {
LL y = (x % n) * (x % n) % n;
if(y == && x != && x != n - ) return false;
x = y;
u = u * ;
}
if(x != ) return false;
}
return true;
} int main() {
// fre();
Eular_Sieve();
int T;
scanf("%d",&T);
while(T --) {
LL n;
scanf("%I64d",&n);
if(n<=){
printf("%d\n",-n);
continue;
}
LL x = sqrt(n);
LL y = sqrt(n) + ;
LL cnt = ,ans = ;
while() {
LL xx = x - cnt;
if(Miller_Rabin(xx)) {
ans = n - xx * xx;
break;
} else {
LL pp = xx;
bool flag = true;
for(int i = ; i < primeCount && i * i <= pp; i ++) {
int cou = ;
while(pp % primeList[i] == ) {
pp = pp / primeList[i];
cou ++;
if(cou>=){
break;
}
}
if(cou >= ) {
flag = false;
break;
}
}
if(flag == true) {
ans = n - xx * xx;
break;
}
}
cnt ++;
}
cnt = ;
while() {
LL yy = y + cnt;
if(yy * yy - n >= ans) break;
if(Miller_Rabin(yy)) {
ans = min(ans,yy * yy - n);
break;
} else {
LL pp = yy;
bool flag = true;
for(int i = ; i < primeCount && i * i <= pp; i ++) {
int cou = ;
while(pp % primeList[i] == ) {
pp = pp / primeList[i];
cou ++;
if(cou>=){
break;
}
}
if(cou >= ) {
flag = false;
break;
}
}
if(flag == true) {
ans = min(ans,yy * yy - n);
break;
}
}
cnt ++;
}
printf("%I64d\n",ans);
}
return ;
}

HDU5778 abs的更多相关文章

  1. BestCoder Round #85 hdu5778 abs(素数筛+暴力)

    abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...

  2. hdu-5778 abs(暴力枚举)

    题目链接: abs Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Pro ...

  3. 【HDU5778】abs(数学)

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

  4. Math.abs()方法 取绝对值

    定义和用法 abs() 方法可返回数的绝对值. 语法 Math.abs(x) 参数 描述 x 必需.必须是一个数值. 返回值 x 的绝对值. 实例 在本例中,我将取得正数和负数的绝对值: <sc ...

  5. 实时控制软件设计作业_01——汽车ABS系统分析

    制动防抱死系统(antilock brake system)简称ABS.作用就是在汽车制动时,自动控制制动器制动力的大小,使车轮不被抱死,处于边滚边滑(滑移率在20%左右)的状态,以保证车轮与地面的附 ...

  6. 实时控制软件设计第一周作业-汽车ABS软件系统案例分析

    汽车ABS软件系统案例分析 ABS 通过控制作用于车轮制动分泵上的制动管路压力,使汽车在紧急刹车时车轮不会抱死,这样就能使汽车在紧急制动时仍能保持较好的方向稳定性. ABS系统一般是在普通制动系统基础 ...

  7. 汽车ABS系统-第一周作业

    ABS系统也成防抱死系统(Anti-lock Braking System),由罗伯特·博世有限公司所开发的一种在摩托车和汽车中使用,它会根据各车轮角速度信号,计算得到车速.车轮角减速度.车轮滑移率: ...

  8. all ,any,abs的使用

    #!/usr/bin/env python #all循环参数,如果每个元素都为真,那么all的返回值为真 r = all([True,'sad','asd']) print(r) #any 只有一个真 ...

  9. Math.abs(~2018),掌握规律即可!

    Math.abs(~2018) 某前端群的入门问题长姿势了,一个简单的入门问题却引发了我的思考,深深的体会到自己在学习前端技术的同时忽略遗忘了一些计算机的基础知识. 对于 JS Math对象没什么可说 ...

随机推荐

  1. java nio2

    Buffer的基本用法 使用Buffer读写数据一般遵循以下四个步骤: 写入数据到Buffer 调用flip()方法 从Buffer中读取数据 调用clear()方法或者compact()方法 当向b ...

  2. 关于NGUI制作图集在低内存设备上的注意事项

    正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...

  3. ubuntu 折腾之路

    aptitude search :search for the lib...and their realtions. apt-get install :install the app apt-get ...

  4. 转Struts 权限控制

    权限最核心的是业务逻辑,具体用什么技术来实现就简单得多. 通常:用户与角色建立多对多关系,角色与业务模块构成多对多关系,权限管理在后者关系中. 对权限的拦截,如果系统请求量大,可以用Struts2拦截 ...

  5. BZOJ 2326 数学作业(矩阵)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2326 题意:定义Concatenate(1,N)=1234567……n.比如Concat ...

  6. Provider Pattern提供者模式和策略模式

    http://www.codeproject.com/Articles/18222/Provider-Pattern Introduction Provider pattern is one of t ...

  7. Jqgrid入门-结合Struts2+json实现数据展示(五)

    DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了.分页表格的数据操作难点就是数据展现.至于增删改直接用hibernate原生的方法实现即可.         初步分析:表格要实现分页,那么 ...

  8. android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示

    Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...

  9. [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互

    [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互,脚本调用底层自定义的方法函数. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterF ...

  10. 【Java学习笔记】Hello world

    package aaa; public class aaa { public static void main(String args[]){ System.out.println("hel ...