AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC172-D Sum of Divisors


問題へのリンク


C#のソース

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

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4");
            //23
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("100");
            //26879
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10000000");
            //838627288460105
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long N = long.Parse(InputList[0]);
        long UB = N;

        long[] YakusuuCntArr = new long[UB + 1];
        for (long I = 1; I <= UB; I++) {
            for (long J = I; J <= UB; J += I) {
                YakusuuCntArr[J]++;
            }
        }

        long Answer = 0;
        for (long I = 1; I <= UB; I++) {
            Answer += I * YakusuuCntArr[I];
        }
        Console.WriteLine(Answer);
    }
}


解説

エラトステネスの篩のアルゴリズムを応用してます。