結果

問題 No.300 平方数
ユーザー norioc
提出日時 2015-11-13 22:40:55
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 37 ms / 1,000 ms
コード長 1,451 bytes
コンパイル時間 2,574 ms
コンパイル使用メモリ 112,588 KB
実行使用メモリ 27,396 KB
最終ジャッジ日時 2024-09-13 14:42:18
合計ジャッジ時間 5,331 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

static class Prime {
    public static List<int> Sieve(int n) {
        var tab = new bool[n+1];
        var ps = new List<int>();
        if (n >= 2) ps.Add(2);
        for (int i = 3; i <= n; i += 2) {
            if (!tab[i]) { // i is prime
                ps.Add(i);
                for (int j = i+i; j <= n; j += i) {
                    tab[j] = true;
                }
            }
        }
        return ps;
    }
}

class Program {
    static string ReadLine() { return Console.ReadLine(); }
    static int ReadInt() { return int.Parse(ReadLine()); }
    static int[] ReadInts() { return ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return ReadLine().Split(); }



    static void Main() {
        var x = long.Parse(ReadLine());
        var primes = Prime.Sieve(1000000);

        var d = new Dictionary<long, int>();
        foreach (int p in primes) {
            if (p * p > x) break;
            while (x % p == 0) {
                if (!d.ContainsKey(p)) d[p] = 0;
                d[p]++;

                x /= p;
            }
        }

        if (x > 1) {
            if (d.ContainsKey(x)) d[x]++;
            else d[x] = 1;
        }

        long ans = 1;
        foreach (var k in d) {
            if (k.Value % 2 == 1) {
                ans *= k.Key;
            }
        }
        Console.WriteLine(ans);
    }
}
0