HDU5778 abs(数论)

题目点我点我点我


解题思路:先把x开根号,往两边暴力搜索符合条件的数,取最小值即可。


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
inline void read(LL &n){n=0;bool _minus=false;while(true){unsigned char c=getchar();if(c==' '||c=='\n')break;if(c=='-'){_minus=true;continue;}n=n*10+c-'0';}if(_minus)n*=-1;}
inline void read(int &n){n=0;bool _minus=false;while(true){unsigned char c=getchar();if(c==' '||c=='\n')break;if(c=='-'){_minus = true;continue;}n=n*10+c-'0';}if(_minus)n*=-1;}
//#pragma comment(linker, "/STACK:102400000,102400000")

bool isPrime[1000005];
int PrimeList[100000];
int cnt;

int Eular(int x)
{
    for(int i = 0; i< 100005; i++)
    {
        isPrime[i] = true;
    }
    int Primecount = 0;
    for(int i = 2; i<= x; i++)
    {
        if(isPrime[i])
        {
            Primecount++;
            PrimeList[Primecount] = i;
        }
        for(int j = 1; j<= Primecount; j++)
        {
            if(i*PrimeList[j] > x) break;
            isPrime[i*PrimeList[j]] = false;
            if(i%PrimeList[j] == 0 ) break;
        }
    }
    //for(int i = 1; i<= Primecount; i++) cout << PrimeList[i] << endl;
    return Primecount;
}

int check(LL x)
{
    for(int i=1;i<=cnt;i++)
    {
        LL k = PrimeList[i];
        if(x%k==0)
        {
            x /= k;
            if(x%k==0)return 0;
        }
    }
    return 1;
}

LL x,y;

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
    cnt = Eular(1000000);
	int t;
	read(t);
	while(t--)
    {
        read(x);
        y = floor(sqrt(x));
        LL p = y, q = y + 1;
        while(check(p)==0)p--;
        while(check(q)==0)q++;
        LL ans = abs(q*q-x);
        if(p>=2)ans = min(ans,abs(p*p-x));
        printf("%I64d\n",ans);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值