結果

問題 No.1022 Power Equation
コンテスト
ユーザー kakel-san
提出日時 2025-11-14 20:09:26
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 2,161 bytes
コンパイル時間 8,128 ms
コンパイル使用メモリ 169,728 KB
実行使用メモリ 212,356 KB
最終ジャッジ日時 2025-11-14 20:09:44
合計ジャッジ時間 10,221 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #
raw source code

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();

    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var t = NN;
        var ans = new long[t];
        for (var u = 0; u < t; ++u)
        {
            var n = NN;
            ans[u] = Power(n);
        }
        WriteLine(string.Join("\n", ans));
    }
    static long Power(int n)
    {
        var ans = 0L;
        // a=1,c=1
        ans += (long)n * n;
        // 1 < a = c
        ans += (n - 1L) * n;
        // 1 < a < c
        //x^i = a, x^j = c, j > 1
        for (var x = 2; x * x <= n; ++x)
        {
            var list = new List<int>() { x };
            while (list[^1] <= n)
            {
                var next = list[^1] * x;
                if (next <= n && next / x == list[^1]) list.Add(next);
                else break;
            }
            for (var p = 0; p < list.Count; ++p) for (var q = p + 1; q < list.Count; ++q)
                {
                    // i = p + 1, j = q + 1
                    // i * b == j * d
                    var i = p + 1;
                    var j = q + 1;
                    var gcd = GCD(i, j);
                    if (gcd != 1) continue;
                    
                    var lcm = LCM(p + 1, q + 1);
                    ans += n / j * 2;
                }
        }
        return ans;
    }
    static int LCM(int a, int b)
    {
        var gcd = GCD(a, b);
        return a / gcd * b;
    }
    static int GCD(int a, int b)
    {
        if (a < b) return GCD(b, a);
        if (a % b == 0) return b;
        return GCD(b, a % b);
    }
}
0