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对象没什么可说 ...
随机推荐
- BeanFactory 和 ApplicationContext
Spring通过一个配置文件描述Bean及Bean直接的依赖关系,利用Java语言的反射功能实例化Bean并建立Bean之间的依赖关系.Sprig的IoC容器在完成这些底层工作的基础上,还提供了Bea ...
- Java类型
Java类型 本地类型 描述boolean jboolean C/C++8位整型byte jbyte C/C++带符号的8位整型c ...
- Drawable(5)关于从资源文件构造的Drawable不显示
要给它设置个bounds才可以 TextView noticeHeaderView; TextView headerRefreshText; ProgressBar headerRefreshPgrs ...
- C# 写入XML文档三种方法详细介绍
三个类将同样的xml内容写入文档,介绍了如何使用XmlDocument类对XML进行操作,以及如何使用LINQ to XML对XML进行操作. 它们分别使用了XmlDocument类和XDocum ...
- Android Studio上的几个插件
转载:http://blog.csdn.net/maosidiaoxian/article/details/44992655 以下所有插件都可以在Idea的插件库中找到,如果你与我一样在Android ...
- git设置对比工具
windows下设置 beyond compare 3 为 git 的对比工具. 首先需要先安装 beyond compare 3 工具,切记需要安装安装版的,不要搞绿色版的. mac下使用 Kal ...
- TCSRM 593 div2(1000)(dp)
Problem Statement The pony Rainbow Dash wants to choose her pet. There are N animals who want t ...
- UVa 1640 (计数) The Counting Problem
题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...
- WebView线性进度条
参考:http://www.cnblogs.com/hubli/p/4835549.html APP会跳转网页时候,请参考:http://blog.csdn.net/raphael55/article ...
- Java Web编程的主要组件技术——Hibernate入门
参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...