HDU5778 abs
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的更多相关文章
- BestCoder Round #85 hdu5778 abs(素数筛+暴力)
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...
- hdu-5778 abs(暴力枚举)
题目链接: abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Pro ...
- 【HDU5778】abs(数学)
BUPT2017 wintertraining(16) #4 C HDU - 5778 题意 给定x,找出使|y-x|最小,且每个质因子都出现两次的y(\(y\le 2\))50组测试数据,\(1\l ...
- Math.abs()方法 取绝对值
定义和用法 abs() 方法可返回数的绝对值. 语法 Math.abs(x) 参数 描述 x 必需.必须是一个数值. 返回值 x 的绝对值. 实例 在本例中,我将取得正数和负数的绝对值: <sc ...
- 实时控制软件设计作业_01——汽车ABS系统分析
制动防抱死系统(antilock brake system)简称ABS.作用就是在汽车制动时,自动控制制动器制动力的大小,使车轮不被抱死,处于边滚边滑(滑移率在20%左右)的状态,以保证车轮与地面的附 ...
- 实时控制软件设计第一周作业-汽车ABS软件系统案例分析
汽车ABS软件系统案例分析 ABS 通过控制作用于车轮制动分泵上的制动管路压力,使汽车在紧急刹车时车轮不会抱死,这样就能使汽车在紧急制动时仍能保持较好的方向稳定性. ABS系统一般是在普通制动系统基础 ...
- 汽车ABS系统-第一周作业
ABS系统也成防抱死系统(Anti-lock Braking System),由罗伯特·博世有限公司所开发的一种在摩托车和汽车中使用,它会根据各车轮角速度信号,计算得到车速.车轮角减速度.车轮滑移率: ...
- all ,any,abs的使用
#!/usr/bin/env python #all循环参数,如果每个元素都为真,那么all的返回值为真 r = all([True,'sad','asd']) print(r) #any 只有一个真 ...
- Math.abs(~2018),掌握规律即可!
Math.abs(~2018) 某前端群的入门问题长姿势了,一个简单的入门问题却引发了我的思考,深深的体会到自己在学习前端技术的同时忽略遗忘了一些计算机的基础知识. 对于 JS Math对象没什么可说 ...
随机推荐
- 手动安装UniDAC的过程
每次安装都不一样,而且有时候挺麻烦的,所以记下来.我这次安装过程是这样的: 0. 修改Source\Delphi7\Make.bat,填入正确的IdeDir 1.找到source\delphi7目录, ...
- Redis是什么?
1. Redis是什么 这个问题的结果影响了我们怎么用Redis.如果你认为Redis是一个key value store, 那可能会用它来代替MySQL;如果认为它是一个可以持久化的cache, 可 ...
- MyEclipse 2014GA 新建 Web Project 并配置 SSH
基本软件配置: 1)MyEclipse 2014GA(JDK:内置 1.7.0.u45:SSH:内置 Struts2.1.Spring3.1 和 Hibernate4.1) 2)apache- ...
- 完美配置Tomcat的HTTPS
Tomcat配置HTTPS的文章到处都有,过程也比较简单,随后文中会转一段过来. 但对于启用APR情况下报异常“java.lang.Exception: Connector attribute SSL ...
- web rest api tools
https://chrome.google.com/webstore/search/postman-REST%20Client
- android4.4内核移植
01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...
- ffmpeg 2.8.1 最新版本 VS2013 可调式动态库
ffmpeg 2.8.1 最新版本 VS2013 可调式动态库 由于大多数初学者都在想尽各种版本寻求VC编译调试ffmpeg的版本,我也曾经移植过几个版本的ffmpeg到VC上编译.: 链接所需动态库 ...
- git push提示或错误
当 git 和 gerrit 一起使用的时候,你创建了一个 tag,现在需要 push 到远程仓库,当你没有权限的时候,会出现如下提示: $ git push origin v20150203 Tot ...
- Android提供的LruCache类简介
分类: Android开发 2013-02-06 15:26 26733人阅读 评论(10) 收藏 举报 package android.util; import import /** * A cac ...
- SVN备份及其还原 — dump/load方法
本文中采用最简单的dump/load方法.备份:一个较大的Subsersion版本库想用最少的空间来将它备份下来,用这个命令(请将/repo替换成你的版本库路径)svnadmin dump --del ...