HDU 5778 abs

Problem Description
Given a number x, ask positive integer  y2 , 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 (  1T50 )
For each test case,the single line contains, an integer x (  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就是每个素数因子出现一次的数,可以预处理一下10w的素数,然后直接暴力即可

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, tp, p[N], f[N], t;
LL n, ans;

void init()
{
    t = 0;
    rep(i, 2, N - 1)
    {
        if (!f[i]) p[t++] = i;
        for (int j = 0; j < t&&p[j] * i < N; j++)
        {
            f[i*p[j]] = 1;
            if (i%p[j]) break;
        }
    }
}

bool check(int x)
{
    for (int i = 0, j, k = x; i < t; i++)
    {
        for (j = 0; k%p[i] == 0; j++) k /= p[i];
        if (j > 1) return false;
        if (p[i] * p[i] > k) break;
    }
    ans = min(ans, abs(n - 1LL * x*x));
    return true;
}

int main()
{
    init();
    scanf("%d", &T);
    while (T--)
    {
        scanf("%I64d", &n);
        LL x = (LL)sqrt(1.0*n);
        LL y = x + 1;
        ans = INF;
        while (x >= 2 && !check(x)) --x;
        while (!check(y)) ++y;
        printf("%I64d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值