結果

問題 No.2074 Product is Square ?
ユーザー dn6049949dn6049949
提出日時 2022-09-16 23:24:00
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,108 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,788 KB
実行使用メモリ 78,820 KB
最終ジャッジ日時 2024-06-01 14:11:11
合計ジャッジ時間 9,581 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
56,284 KB
testcase_01 AC 699 ms
78,392 KB
testcase_02 AC 681 ms
78,504 KB
testcase_03 AC 688 ms
78,820 KB
testcase_04 AC 668 ms
78,328 KB
testcase_05 AC 701 ms
78,328 KB
testcase_06 AC 656 ms
78,452 KB
testcase_07 AC 689 ms
78,552 KB
testcase_08 AC 659 ms
78,704 KB
testcase_09 AC 625 ms
78,136 KB
testcase_10 AC 616 ms
78,736 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 69 ms
75,808 KB
testcase_32 AC 45 ms
56,440 KB
testcase_33 AC 96 ms
76,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def IR(n):
    return [I() for _ in range(n)]
def LIR(n):
    return [LI() for _ in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def gcd(a, b):
    while a:
        a, b = b%a, a
    return b


test_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23]
def is_prime(n):
    if n == 2:
        return 1
    if n == 1 or n%2 == 0:
        return 0

    m = n - 1
    lsb = m & -m
    s = lsb.bit_length()-1
    d = m // lsb
    for a in test_numbers:
        if a == n:
            continue
        x = pow(a,d,n)
        r = 0
        if x == 1:
            continue
        while x != m:
            x = pow(x,2,n)
            if x == 1 or r == s:
                return 0
            r += 1
    return 1


def find_prime_factor(n):
    m = 2*int(2**(((n.bit_length()-1)>>2)/2))

    for c in range(1,n):
        f = lambda a: (pow(a,2,n)+c)%n
        y = 0
        g = q = r = 1
        while g == 1:
            x = y
            for _ in range(r):
                y = f(y)
            k = 0
            while k < r and g == 1:
                ys = y
                for _ in range(min(m, r-k)):
                    y = f(y)
                    q = q*abs(x-y)%n
                g = gcd(q,n)
                k += m
            r <<= 1
        if g == n:
            g = 1
            y = ys
            while g == 1:
                y = f(y)
                g = gcd(abs(x-y),n)
        if g == n:
            continue
        if is_prime(g):
            return g
        elif is_prime(n//g):
            return n//g
        else:
            n = g


def factorize(n):
    res = {}
    for p in range(2,1000):
        if p*p > n:
            break
        if n%p:
            continue
        s = 0
        while n%p == 0:
            n //= p
            s += 1
        res[p] = s

    while not is_prime(n) and n > 1:
        p = find_prime_factor(n)
        s = 0
        while n%p == 0:
            n //= p
            s += 1
        res[p] = s
    if n > 1:
        res[n] = 1
    return res


def main():
    n = I()
    a = LI()
    x = 1
    for i in a:
        x *= i
    s = len(str(x))
    l = 10**max(0,s//2-10)
    r = 10**(s//2+10)
    while l+1 < r:
        m = (l+r) >> 1
        if m*m <= x:
            l = m
        else:
            r = m
    a.sort()
    s = defaultdict(lambda : 0)
    for i in a[:-1]:
        f = factorize(i)
        for i,j in f.items():
            s[i] ^= j&1
    a = a[-1]
    for i,j in s.items():
        if j:
            if a%i:
                print("No")
                return
            a //= i
    l = 0
    r = a+1
    while l+1 < r:
        m = (l+r) >> 1
        if m**2 <= a:
            l = m
        else:
            r = m

    if l**2 == a:
        print("Yes")
    else:
        print("No")
    return


if __name__ == "__main__":
    for _ in range(I()):
        main()
0