zoukankan      html  css  js  c++  java
  • abs 暴力

    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;
        }
    }
  • 相关阅读:
    maven-仓库
    maven-坐标与依赖
    maven-认识
    mysql-介绍
    redis-主从复制
    UVA
    UVA
    UVA
    UVA
    [ Java学习 ] 查阅资料整理 004
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7395435.html
Copyright © 2011-2022 走看看