HDU 5778 abs(暴力枚举)——BestCoder Round #85 1003

来源:互联网 发布:湖北省软件行业协会 编辑:程序博客网 时间:2024/06/05 19:54

传送门

abs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1474    Accepted Submission(s): 511


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
 

Source
BestCoder Round #85

题目大意:

给定一个数 x,求正整数y>=2,使得满足以下条件:1.yx 的绝对值最小2.y 的质因数分解式中每个质因数均恰好出现2次。

解题思路:
这个题目就是一个暴力,因为 每个质因数恰好出现两次 所以 y 一定是一个完全平方数,然后我们就暴力枚举 x 周围的数就行了。
PS: 当时没有特判 13 内的数,导致被 hack了,我恨呐…

My Code

/**2016 - 08 - 05 下午Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF = 1e9+5;const int MAXN = 1e6+5;const LL MOD = 1e4+7;const double eps = 1e-7;const double PI = acos(-1);using namespace std;bool Judge(LL x){    for(LL i=2; i*i<=x; i++)    {        int sum = 0;        while(x%i==0)        {            sum++;            x /= i;        }        if(sum > 1)            return 0;    }    return 1;}int main(){    int T;    LL x;    cin>>T;    while(T--)    {        cin>>x;        if(x <= 3LL)///特判            cout<<4LL-x<<endl;        else        {            LL p1 = (LL)sqrt(x);            LL p2 = p1 + 1;            while(1)            {                if(Judge(p1))                    break;                p1--;            }            while(1)            {                if(Judge(p2))                    break;                p2++;            }            LL ans1 = p1*p1;            LL ans2 = p2*p2;            cout<<min(abs(ans1-x), abs(ans2-x))<<endl;        }    }    return 0;}
0 0