abs

来源:互联网 发布:淘宝服装店铺头像 编辑:程序博客网 时间:2024/05/17 06:33
链接: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 100010bool 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
原创粉丝点击