結果

問題 No.2074 Product is Square ?
ユーザー みここみここ
提出日時 2022-12-13 19:18:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,722 ms / 2,000 ms
コード長 2,735 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 79,580 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 03:49:05
合計ジャッジ時間 49,717 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
5,248 KB
testcase_01 AC 1,241 ms
5,376 KB
testcase_02 AC 1,259 ms
5,376 KB
testcase_03 AC 1,244 ms
5,376 KB
testcase_04 AC 1,243 ms
5,376 KB
testcase_05 AC 1,249 ms
5,376 KB
testcase_06 AC 1,254 ms
5,376 KB
testcase_07 AC 1,246 ms
5,376 KB
testcase_08 AC 1,224 ms
5,376 KB
testcase_09 AC 1,236 ms
5,376 KB
testcase_10 AC 1,233 ms
5,376 KB
testcase_11 AC 1,600 ms
5,376 KB
testcase_12 AC 1,606 ms
5,376 KB
testcase_13 AC 1,644 ms
5,376 KB
testcase_14 AC 1,722 ms
5,376 KB
testcase_15 AC 1,602 ms
5,376 KB
testcase_16 AC 1,630 ms
5,376 KB
testcase_17 AC 1,635 ms
5,376 KB
testcase_18 AC 1,706 ms
5,376 KB
testcase_19 AC 1,598 ms
5,376 KB
testcase_20 AC 1,621 ms
5,376 KB
testcase_21 AC 1,644 ms
5,376 KB
testcase_22 AC 1,714 ms
5,376 KB
testcase_23 AC 1,602 ms
5,376 KB
testcase_24 AC 1,622 ms
5,376 KB
testcase_25 AC 1,645 ms
5,376 KB
testcase_26 AC 1,703 ms
5,376 KB
testcase_27 AC 1,581 ms
5,376 KB
testcase_28 AC 1,571 ms
5,376 KB
testcase_29 AC 1,615 ms
5,376 KB
testcase_30 AC 1,713 ms
5,376 KB
testcase_31 AC 101 ms
5,376 KB
testcase_32 AC 16 ms
5,376 KB
testcase_33 AC 1,596 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
typedef long long ll;

long long gcd(long long n, long long m)
{
    if (m == 0)
    {
        return n;
    }
    return gcd(m, n % m);
}

struct prime_sturcture
{
    int max_n;
    std::vector<bool> p;
    std::vector<int> prime_list;

    prime_sturcture(int max_n) : max_n(max_n), p(max_n + 1, true)
    {
        p[0] = p[1] = false;
        for (int i = 2; i <= max_n; i++)
        {
            if (p[i])
            {
                prime_list.push_back(i);
                for (int j = i * 2; j <= max_n; j += i)
                {
                    p[j] = false;
                }
            }
        }
    }

    bool is_prime(int n)
    {
        assert(n <= max_n);
        return p[n];
    }

    int size()
    {
        return prime_list.size();
    }

    const int operator[](int i) const
    {
        assert(i < (int)prime_list.size());
        return prime_list[i];
    }

    std::vector<int>::const_iterator begin()
    {
        return prime_list.begin();
    }

    std::vector<int>::const_iterator end()
    {
        return prime_list.end();
    }
};

bool is_square(ll x)
{
    ll left = -1, right = 1000000001;
    while (right - left > 1)
    {
        ll mid = (right + left) / 2;
        if (mid * mid >= x)
        {
            right = mid;
        }
        else
        {
            left = mid;
        }
    }
    return right * right == x;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    prime_sturcture prime(1000000);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        bool b[1000005]{0};
        ll a[1005];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            for (int p : prime)
            {
                if (a[i] % p == 0)
                {
                    while (a[i] % ((ll)p * p) == 0)
                    {
                        a[i] /= p * p;
                    }
                    if (a[i] % p == 0)
                    {
                        a[i] /= p;
                        b[p] ^= 1;
                    }
                }
            }
            if (is_square(a[i]))
            {
                a[i] = 1;
            }
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                ll g = gcd(a[i], a[j]);
                a[i] /= g;
                a[j] /= g;
            }
        }
        bool f = false;
        for (int p : prime)
        {
            f |= b[p];
        }
        for (int i = 0; i < n; i++)
        {
            f |= (a[i] > 1);
        }
        cout << (!f ? "Yes\n" : "No\n");
    }
}
0