码迷,mamicode.com
首页 > 其他好文 > 详细

abs 暴力

时间:2017-08-19 11:07:28      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:cout   function   factor   nim   pac   std   int   cstring   func   

Given a number x, ask positive integer y2y≥2, 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.

InputThe first line of input is an integer T ( 1T501≤T≤50) 
For each test case,the single line contains, an integer x ( 1x10181≤x≤1018)
OutputFor each testcase print the absolute value of y - xSample Input

5
1112
4290
8716
9957
9095

Sample Output

23
65
67
244
70

y是完全平方数,枚举根号y即可,注意左右两边的最小值取小的那个
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<fstream>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;

typedef long long LL;
#define INF 0x9f9f9f9f
LL T, n;
bool check(LL t)
{
    for (LL i = 2; i*i <= t; i++)
    {
        LL cnt = 0;
        while (t%i == 0)
            cnt++, t /= i;
        if (cnt > 1) return false;
    }
    return true;
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin >> T;
    while (T--)
    {
        cin >> n;
        if (n <= 4)
        {
            cout << 4 - n << endl;
            continue;
        }
        LL dis = 0, tmp = sqrt(n), ans;
        while (1)
        {
            LL a = tmp - dis;
            if (a*a < n && check(a))
            {
                ans =  n - a * a;
                break;
            }
            dis++;
        }
        dis = 0;
        while (1)
        {
            LL a = tmp + dis;
            if (a*a >= n && check(a))
            {
                ans = min(ans, (a*a - n));
                break;
            }
            dis++;
        }
        cout << ans << endl;
    }
}

 

abs 暴力

标签:cout   function   factor   nim   pac   std   int   cstring   func   

原文地址:http://www.cnblogs.com/joeylee97/p/7395435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!