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;
        }
    }
  • 相关阅读:
    springMVC的自定义类型的转换器
    pl/sql
    oracle中的函数
    Oracle基础
    跳台阶算法题
    红黑树
    优先队列
    堆排序
    H5页面,输入框的光标,如果页面上下滑动光标停留在页面上,除了输入框外,松手过了一段时间才跑回输入框里面
    正则:判断为数字,输入的金额整数位不得超过9位,小数位不得超过2位!
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7395435.html
Copyright © 2011-2022 走看看