HDU 5778 abs

Problem Description

Given a number x, ask positive integer y\geq 2y2, that satisfy the following conditions:

  1. The absolute value of y - x is minimal
  2. To prime factors decomposition of Y, every element factor appears two times exactly.
Input

The first line of input is an integer T ( 1\leq T \leq501T50) For each test case,the single line contains, an integer x ( 1\leq x \leq {10} ^ {18}1x1018)

Output

For each testcase print the absolute value of y - x

Sample Input
5
1112
4290
8716
9957
9095
Sample Output
23
65
67
244
70

中文描述:

问题描述
给定一个数x,求正整数y\geq 2y2,使得满足以下条件:
1.y-x的绝对值最小
2.y的质因数分解式中每个质因数均恰好出现2次。
输入描述
第一行输入一个整数T(1\leq T\leq 501T50)
每组数据有一行,一个整数x(1\leq x\leq {10}^{18}1x1018)
输出描述
对于每组数据,输出一行y-x的最小绝对值
输入样例
5
1112
4290
8716
9957
9095
输出样例
23
65
67
244
70

#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
typedef long long ll;
bool divide(ll n){
	for(int i=2;i<=sqrt(n);i++){
		if(n%i==0){
			while(n%i==0){
				n=n/i;
				if(n%i==0) return false;
			}
		}
	}
	return true;
} 
ll sqr(ll x){
	return x*x;
}
int main(){
//	freopen("3.in","r",stdin);
//	freopen("3.out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--){
		ll x;
		scanf("%lld",&x);
		ll x1=sqrt(x);
		ll x2=x1+1;
		if(x<4) printf("%lld\n",4-x);
		else{
			while(!divide(x1)){
				x1--;
			}
			while(!divide(x2)){
				x2++;
			}
			ll ans=min(abs(sqr(x1)-x),abs(sqr(x2)-x));
			printf("%lld\n",ans);
		}
	}
}


题意其实是求一个y-x的绝对值最小,并且y中的质因子次数要么是0要么是2;

x的数量级是1e18,所以我们可以考虑把x开方后,寻找开方后的x附近的某个数,这个数质因子只能出现过1次;

将开方后的x取整,令x1为左范围,x2为右范围;

然后把范围都扩大到因子只出现一次的数就可以了

开始我其实是打算暴力在x开方周围解决的,后来对样例进行测试计算的时候发现了规律,说实话也不知道是自己运气好还是真的有能力去解决。。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值