結果

問題 No.2074 Product is Square ?
ユーザー KumaTachiRenKumaTachiRen
提出日時 2022-09-17 23:33:11
言語 C#
(.NET 8.0.203)
結果
TLE  
実行時間 -
コード長 17,349 bytes
コンパイル時間 13,322 ms
コンパイル使用メモリ 148,224 KB
実行使用メモリ 142,240 KB
最終ジャッジ日時 2023-08-23 17:56:55
合計ジャッジ時間 15,880 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
23,960 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 134 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System.Diagnostics;
using System;
using System.Linq;
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static Functions;

using N = System.Int64;

static class Program
{
    static public void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var solver = new Solver();
        solver.Solve();
        Console.Out.Flush();
    }
}

public class Solver
{
    public void Solve()
    {
        int t = ri;
        var ans = new string[t];
        for (int q = 0; q < t; q++)
        {
            int n = ri;
            var d = new Dictionary<long, int>();
            for (int i = 0; i < n; i++)
            {
                var f = PrimeFactoring(rl);
                foreach (var k in f.Keys)
                {
                    if (d.ContainsKey(k)) d[k] ^= (int)(f[k] & 1);
                    else d.Add(k, (int)(f[k] & 1));
                }
            }
            bool ok = true;
            foreach (var p in d.Keys) ok &= d[p] == 0;
            ans[q] = ok ? "Yes" : "No";
        }
        WriteJoin("\n", ans);
    }

    bool IsPrimeMR(long n)
    {
        if ((n & 1) == 0) return n == 2;
        if (n <= 1) return false;
        long d = n - 1;
        while ((d & 1) == 0) d >>= 1;
        var aarr = new long[] { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
        var mont = new Montgomery(n);
        foreach (var a in aarr)
        {
            if (n <= a) break;
            long t = d, x = a, y = 1;
            while (t > 0)
            {
                if ((t & 1) == 1) y = mont.Mul(x, y);
                x = mont.Mul(x, x);
                t >>= 1;
            }
            t = d;
            while (t != n - 1 && y != 1 && y != n - 1)
            {
                y = mont.Mul(y, y);
                t <<= 1;
            }
            if (y != n - 1 && (t & 1) == 0) return false;
        }
        return true;
    }
    long FindFactorRho(long n)
    {
        var mont = new Montgomery(n);
        long m = 1;
        while (m * m * m * m * m * m * m * m < n) m++;
        for (int c = 1; c < 10000; c++)
        {
            long x = 1, y = c, ys = 0, q = 1;
            long g = 1;
            for (int r = 1; g == 1; r <<= 1)
            {
                x = y;
                for (int i = 0; i < r; ++i) y = mont.Conv(mont.Mul(y, y) + c);
                for (long k = 0; g == 1 && k < r; k += m)
                {
                    ys = y;
                    for (int i = 0; i < m && i < r - k; ++i)
                    {
                        y = mont.Conv(mont.Mul(y, y) + c);
                        q = mont.Mul(q, Math.Abs(x - y));
                    }
                    g = Math.Abs(Gcd(q, n));
                }
            }
            if (g == n)
            {
                do
                {
                    ys = mont.Conv(mont.Mul(ys, ys) + c);
                    g = Gcd(Math.Abs(x - ys), n);
                }
                while (g == 1);
            }
            if (g != n) return g;
        }
        return n;
    }
    SortedDictionary<long, long> PrimeFactoring(long n)
    {
        long i = 2;
        var ret = new SortedDictionary<long, long>();
        while (i * i <= n)
        {
            long k = 0;
            while (n % i == 0)
            {
                n /= i;
                k++;
            }
            if (k > 0) ret.Add(i, k);
            i += 1 + (i & 1);
            if (i == 101 && n >= (1 << 20))
            {
                while (n > 1)
                {
                    if (IsPrimeMR(n))
                    {
                        ret.Add(n, 1);
                        n = 1;
                    }
                    else
                    {
                        long j = FindFactorRho(n);
                        k = 0;
                        while (n % j == 0)
                        {
                            n /= j;
                            k++;
                        }
                        ret.Add(j, k);
                    }
                }
            }
        }
        if (n > 1) ret.Add(n, 1);
        return ret;
    }
    class Montgomery
    {
        long n, nr, mask;
        UInt128 r2;
        int nb;
        public Montgomery(long _n)
        {
            n = _n;
            nb = 0;
            while (n >= (1L << nb)) nb++;
            r2 = ((UInt128)1 << (nb * 2)) % n;
            mask = (1L << nb) - 1;
            nr = 0;
            long t = 0, vi = 1;
            for (int i = 0; i < nb; i++)
            {
                if ((t & 1) == 0)
                {
                    t += n;
                    nr += vi;
                }
                t >>= 1;
                vi <<= 1;
            }
        }
        public long Reduction(UInt128 t)
        {
            UInt128 c = t * nr;
            c &= mask;
            c *= n;
            c += t;
            c >>= nb;
            if (c >= n) c -= n;
            return c;
        }
        public long Conv(UInt128 a) => Reduction(Reduction(a) * r2);
        public long Mul(UInt128 a, UInt128 b) => Reduction(Reduction(a * b) * r2);
        public long Pow(UInt128 a, long b)
        {
            long p = Reduction(a * r2);
            long x = Reduction(r2);
            long y = b;
            while (y > 0)
            {
                if ((y & 1) == 1) x = Reduction(x * p);
                p = Reduction(p * p);
                y >>= 1;
            }
            return Reduction(x);
        }
    }
    struct UInt128
    {
        private const int l = 64;
        private const ulong MaxValue = ulong.MaxValue;
        public ulong t, b;
        public UInt128(long n) : this(0, (ulong)n) { }
        public UInt128(ulong n) : this(0, n) { }
        public UInt128(long _t, long _b) : this((ulong)_t, (ulong)_b) { }
        public UInt128(ulong _t, ulong _b)
        {
            t = _t;
            b = _b;
        }
        public static implicit operator UInt128(int n) => new UInt128(n);
        public static implicit operator UInt128(long n) => new UInt128(n);
        public static implicit operator UInt128(ulong n) => new UInt128(n);
        public static implicit operator ulong(UInt128 n) => n.b;
        public static implicit operator long(UInt128 n) => (long)n.b;
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType()) return false;
            var x = (UInt128)obj;
            return x.t == t && x.b == b;
        }
        public override int GetHashCode() => t.GetHashCode() ^ b.GetHashCode();
        public override string ToString() => $"UInt128({t},{b})";
        public static bool operator ==(UInt128 x, UInt128 y) => Equals(x, y);
        public static bool operator !=(UInt128 x, UInt128 y) => !Equals(x, y);
        public static bool operator <(UInt128 x, UInt128 y) => x.t != y.t ? x.t < y.t : x.b < y.b;
        public static bool operator >(UInt128 x, UInt128 y) => x.t != y.t ? x.t > y.t : x.b > y.b;
        public static bool operator <=(UInt128 x, UInt128 y) => x.t != y.t ? x.t < y.t : x.b <= y.b;
        public static bool operator >=(UInt128 x, UInt128 y) => x.t != y.t ? x.t > y.t : x.b >= y.b;
        public static UInt128 operator +(UInt128 x, UInt128 y) => x.b <= MaxValue - y.b ? new UInt128(x.t + y.t, x.b + y.b) : new UInt128(x.t + y.t + 1, x.b + y.b);
        public static UInt128 operator -(UInt128 x, UInt128 y) => x.b >= y.b ? new UInt128(x.t - y.t, x.b - y.b) : new UInt128(x.t - y.t - 1, x.b + (MaxValue - y.b) + 1);
        public static UInt128 operator *(UInt128 x, UInt128 y)
        {
            const ulong mask = (1UL << (l / 4)) - 1;
            var p = new ulong[8];
            var q = new ulong[8];
            for (int i = 0; i < 4; i++)
            {
                p[i] = (x.b >> (l / 4 * i)) & mask;
                p[i + 4] = (x.t >> (l / 4 * i)) & mask;
                q[i] = (y.b >> (l / 4 * i)) & mask;
                q[i + 4] = (y.t >> (l / 4 * i)) & mask;
            }
            var r = new ulong[8];
            for (int i = 0; i < 8; i++)
                for (int j = 0; i + j < 8; j++)
                    r[i + j] += p[i] * q[j];
            for (int i = 0; i < 7; i++) r[i + 1] += r[i] >> (l / 4);
            for (int i = 0; i < 8; i++) r[i] &= mask;
            ulong t = 0, b = 0;
            for (int i = 0; i < 4; i++)
            {
                b |= r[i] << (l / 4 * i);
                t |= r[i + 4] << (l / 4 * i);
            }
            return new UInt128(t, b);
        }
        public static UInt128 operator /(UInt128 x, UInt128 y)
        {
            ulong a = (ulong)1 << (l - 1), b = 0;
            var t = y >> 1;
            while (t > 0)
            {
                if (a == 1)
                {
                    a = 0;
                    b = (ulong)1 << (l - 1);
                }
                else if (a > 0) a >>= 1;
                else b >>= 1;
                t >>= 1;
            }
            const ulong mask = (1UL << (l / 4)) - 1;
            ulong[] p, q, r;
            ulong aprv = a, bprv = b;
            while (true)
            {
                p = new ulong[16]; q = new ulong[16];
                for (int i = 0; i < 4; i++)
                {
                    p[i] = (y.b >> (l / 4 * i)) & mask;
                    p[i + 4] = (y.t >> (l / 4 * i)) & mask;
                    q[i] = (b >> (l / 4 * i)) & mask;
                    q[i + 4] = (a >> (l / 4 * i)) & mask;
                }
                r = new ulong[16];
                for (int i = 0; i < 16; i++)
                    for (int j = 0; i + j < 16; j++)
                        r[i + j] += p[i] * q[j];
                for (int j = 0; j < 15; j++)
                {
                    r[j + 1] += r[j] >> (l / 4);
                    r[j] &= mask;
                }
                for (int i = 0; i < 8; i++) p[i] = mask - r[i];
                for (int i = 0; i < 8; i++)
                {
                    if (p[i] != mask)
                    {
                        p[i]++;
                        break;
                    }
                    else p[i] = 0;
                }
                p[8]++;
                r = new ulong[16];
                for (int i = 0; i < 16; i++)
                    for (int j = 0; i + j < 16; j++)
                        r[i + j] += p[i] * q[j];
                for (int i = 0; i < 15; i++)
                {
                    r[i + 1] += r[i] >> (l / 4);
                    r[i] &= mask;
                }
                a = 0; b = 0;
                for (int i = 0; i < 4; i++)
                {
                    a |= r[i + 12] << (l / 4 * i);
                    b |= r[i + 8] << (l / 4 * i);
                }
                if (a == aprv && b == bprv) break;
                aprv = a; bprv = b;
            }
            p = new ulong[16]; q = new ulong[16];
            for (int j = 0; j < 4; j++)
            {
                p[j] = (x.b >> (l / 4 * j)) & mask;
                p[j + 4] = (x.t >> (l / 4 * j)) & mask;
                q[j] = (b >> (l / 4 * j)) & mask;
                q[j + 4] = (a >> (l / 4 * j)) & mask;
            }
            r = new ulong[16];
            for (int j = 0; j < 8; j++)
                for (int k = 0; k < 8; k++)
                    r[j + k] += q[j] * p[k];
            for (int j = 0; j < 15; j++)
            {
                r[j + 1] += r[j] >> (l / 4);
                r[j] &= mask;
            }
            a = 0; b = 0;
            for (int j = 0; j < 4; j++)
            {
                a |= r[j + 12] << (l / 4 * j);
                b |= r[j + 8] << (l / 4 * j);
            }
            var n = new UInt128(a, b);
            return x >= y * (n + 1) ? n + 1 : n;
        }
        public static UInt128 operator %(UInt128 x, UInt128 y) => x - (x / y) * y;
        public static UInt128 operator >>(UInt128 x, int d)
        {
            if (d >= 2 * l) return new UInt128(0);
            if (d >= l) return new UInt128(0, x.t >> (d - l));
            return new UInt128(x.t >> d, ((x.t ^ ((x.t >> d) << d)) << (l - d)) | (x.b >> d));
        }
        public static UInt128 operator <<(UInt128 x, int d)
        {
            if (d >= 2 * l) return new UInt128(0);
            if (d >= l) return new UInt128(x.b << (d - l), 0);
            return new UInt128((x.t << d) | ((x.b ^ ((x.b << d) >> d)) >> (l - d)), x.b << d);
        }
        public static UInt128 operator &(UInt128 x, UInt128 y) => new UInt128(x.t & y.t, x.b & y.b);
        public static UInt128 operator |(UInt128 x, UInt128 y) => new UInt128(x.t | y.t, x.b | y.b);
    }


    const long inf = (long)1 << 60;
    int ri { get { return (int)sc.Integer(); } }
    long rl { get { return sc.Integer(); } }
    double rd { get { return sc.Double(); } }
    string rs { get { return sc.Scan(); } }
    public StreamScanner sc = new StreamScanner(Console.OpenStandardInput());

    void WriteJoin<T>(string s, T[] t) { Console.WriteLine(string.Join(s, t)); }
    void WriteJoin<T>(string s, List<T> t) { Console.WriteLine(string.Join(s, t)); }
    void Write<T>(T t) { Console.WriteLine(t.ToString()); }
    void Debug<T>(T t) { Console.WriteLine(t.ToString()); Console.Out.Flush(); }
    void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); }
    void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); }
    void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); }
}

public static class Functions
{
    public static int Popcount(long x)
    {
        x = x - ((x >> 1) & 0x5555555555555555);
        x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
        x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f;
        x = x + (x >> 8);
        x = x + (x >> 16);
        x = x + (x >> 32);
        return (int)(x & 0x0000007f);
    }
    public static int Ctz(long x)
    {
        if (x == 0) return -1;
        return Popcount((x & -x) - 1);
    }
    public static int SafeMod(int x, int m)
    {
        int r = x % m;
        return r < 0 ? r + m : r;
    }
    public static long SafeMod(long x, long m)
    {
        long r = x % m;
        return r < 0 ? r + m : r;
    }
    public static long Floor(long a, long b) => a >= 0 ? a / b : (a + 1) / b - 1;
    public static long Ceil(long a, long b) => a > 0 ? (a - 1) / b + 1 : a / b;
    public static int Gcd(int a, int b) => b == 0 ? a : Gcd(b, a % b);
    public static long Gcd(long a, long b) => b == 0 ? a : Gcd(b, a % b);
    public static void Swap(ref int x, ref int y) { x ^= y; y ^= x; x ^= y; }
    public static void Swap(ref long x, ref long y) { x ^= y; y ^= x; x ^= y; }
    public static void Swap<T>(ref T x, ref T y) { T t = y; y = x; x = t; }
    public static void Chmin(ref int x, int y) => x = Math.Min(x, y);
    public static void Chmin(ref long x, long y) => x = Math.Min(x, y);
    public static void Chmin(ref double x, double y) => x = Math.Min(x, y);
    public static void Chmax(ref int x, int y) => x = Math.Max(x, y);
    public static void Chmax(ref long x, long y) => x = Math.Max(x, y);
    public static void Chmax(ref double x, double y) => x = Math.Max(x, y);
    public static int LowerBound<T, S>(in List<T> list, S val, Func<T, S, int> comp)
    {
        if (list.Count == 0) return -1;
        if (comp(list.Last(), val) <= 0) return list.Count;
        int l = 0, r = list.Count;
        while (l < r)
        {
            int x = (l + r) / 2;
            if (comp(list[x], val) < 0) l = x + 1;
            else r = x;
        }
        return l;
    }
    public static int UpperBound<T, S>(in List<T> list, S val, Func<T, S, int> comp)
    {
        if (list.Count == 0) return 0;
        if (comp(list.Last(), val) < 0) return list.Count;
        int l = 0, r = list.Count;
        while (l < r)
        {
            int x = (l + r) / 2;
            if (comp(list[x], val) <= 0) l = x + 1;
            else r = x;
        }
        return l;
    }
}


public class StreamScanner
{
    public StreamScanner(Stream stream) { str = stream; }
    private readonly Stream str;
    private readonly byte[] buf = new byte[1024];
    private int len, ptr;
    public bool isEof = false;
    public bool IsEndOfStream { get { return isEof; } }
    private byte read()
    {
        if (isEof) throw new EndOfStreamException();
        if (ptr >= len)
        {
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; }
        }
        return buf[ptr++];
    }
    public char Char()
    {
        byte b = 0;
        do b = read();
        while (b < 33 || 126 < b);
        return (char)b;
    }
    public string Scan()
    {
        var sb = new StringBuilder();
        for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b);
        return sb.ToString();
    }
    public long Integer()
    {
        long ret = 0; byte b = 0; var ng = false;
        do b = read();
        while (b != '-' && (b < '0' || '9' < b));
        if (b == '-') { ng = true; b = read(); }
        for (; true; b = read())
        {
            if (b < '0' || '9' < b) return ng ? -ret : ret;
            else ret = ret * 10 + b - '0';
        }
    }
    public double Double() { return double.Parse(Scan()); }
}
0