結果

問題 No.2074 Product is Square ?
ユーザー みここみここ
提出日時 2022-12-13 00:47:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,716 ms / 2,000 ms
コード長 2,218 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 76,956 KB
実行使用メモリ 4,812 KB
最終ジャッジ日時 2023-08-06 19:17:37
合計ジャッジ時間 49,287 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
4,660 KB
testcase_01 AC 1,234 ms
4,632 KB
testcase_02 AC 1,253 ms
4,628 KB
testcase_03 AC 1,239 ms
4,740 KB
testcase_04 AC 1,248 ms
4,564 KB
testcase_05 AC 1,246 ms
4,564 KB
testcase_06 AC 1,249 ms
4,560 KB
testcase_07 AC 1,242 ms
4,664 KB
testcase_08 AC 1,245 ms
4,692 KB
testcase_09 AC 1,237 ms
4,604 KB
testcase_10 AC 1,230 ms
4,656 KB
testcase_11 AC 1,597 ms
4,812 KB
testcase_12 AC 1,607 ms
4,608 KB
testcase_13 AC 1,640 ms
4,656 KB
testcase_14 AC 1,716 ms
4,604 KB
testcase_15 AC 1,600 ms
4,632 KB
testcase_16 AC 1,630 ms
4,564 KB
testcase_17 AC 1,643 ms
4,628 KB
testcase_18 AC 1,714 ms
4,632 KB
testcase_19 AC 1,593 ms
4,648 KB
testcase_20 AC 1,620 ms
4,564 KB
testcase_21 AC 1,642 ms
4,632 KB
testcase_22 AC 1,712 ms
4,632 KB
testcase_23 AC 1,598 ms
4,684 KB
testcase_24 AC 1,622 ms
4,584 KB
testcase_25 AC 1,645 ms
4,560 KB
testcase_26 AC 1,713 ms
4,656 KB
testcase_27 AC 1,596 ms
4,564 KB
testcase_28 AC 1,621 ms
4,688 KB
testcase_29 AC 1,645 ms
4,688 KB
testcase_30 AC 1,709 ms
4,560 KB
testcase_31 AC 101 ms
4,592 KB
testcase_32 AC 15 ms
4,580 KB
testcase_33 AC 1,648 ms
4,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#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);
}

std::vector<int> enum_prime(int n) // containing n
{
    std::vector<int> res;
    if (n <= 1)
    {
        return res;
    }
    std::vector<bool> p(n + 1);
    fill(p.begin() + 2, p.end(), true);
    for (int i = 2; i <= n; i++)
    {
        if (p[i])
        {
            res.push_back(i);
            for (int j = i * 2; j <= n; j += i)
            {
                p[j] = false;
            }
        }
    }
    return res;
}

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);
    vector<int> prime = enum_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