abs

链接:http://acm.hust.edu.cn/vjudge/problem/440426/origin

题目:Given a number x, ask positive integer   , 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.

题意:给出一个数,求距离他最近的一个满足条件的数与他的差。条件是这个数的每个质因子出现两次。

分析:其实是一个分解质因数的题目。首先可以把距离x最近的所有质因数出现两次的问题优化为:距离根号x最近的每个质因数只出现一次的问题。之后对根号x向下向上找到两个满足条件的数,比较差值就好了。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100010

bool factor(ll n)
{
	set<int> s;
	ll t = (ll)sqrt(n);
	bool flag = true;
	for (int i = 2; i <= t&&flag; i++)
	{
		while (n%i==0&&flag)
		{
			n /= i;
			if (s.count(i))
			{
				flag = false;
				break;
			}
			s.insert(i);
		}
	}
	rt flag;
}

int main()
{
	//fr;
	detie;
	int T;
	cin >> T;
	while (T--)
	{
		ll n;
		cin >> n;
		ll tn1 = (ll)sqrt(n);
		ll tn2 = tn1+1;
		ll ans = 1e18;
		while (!factor(tn1)&&tn1>=2)
		{
			tn1--;
		}
		if (tn1!=1)//便捷处理需要小心谨慎。
			ans = min(ans, n - tn1*tn1); 
		while (!factor(tn2) && tn2 <=1000000000)
		{
			tn2++;
		}
		if(tn1!=1000000001)
			ans = min(ans, tn2*tn2 - n);
		cout << ans << endl;
	}
	rt 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值