結果

問題 No.276 連続する整数の和(1)
ユーザー 明智重蔵明智重蔵
提出日時 2015-09-19 05:45:33
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 56 ms / 1,000 ms
コード長 1,561 bytes
コンパイル時間 3,464 ms
コンパイル使用メモリ 107,120 KB
実行使用メモリ 22,728 KB
最終ジャッジ日時 2023-09-26 13:31:31
合計ジャッジ時間 3,850 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
22,696 KB
testcase_01 AC 54 ms
22,728 KB
testcase_02 AC 56 ms
20,588 KB
testcase_03 AC 56 ms
22,692 KB
testcase_04 AC 55 ms
20,568 KB
testcase_05 AC 55 ms
20,680 KB
testcase_06 AC 55 ms
20,588 KB
testcase_07 AC 55 ms
22,616 KB
testcase_08 AC 55 ms
20,656 KB
testcase_09 AC 55 ms
20,652 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

class Program
{
    static string InputPattern = "Input3";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    //No.276 連続する整数の和(1)
    static void Main()
    {
        List<string> InputList = GetInputList();
        long N = long.Parse(InputList[0]);

        //Aから連続する3つの数の和は、
        //A+A+1+A+2 = 3A+3 = 3*(A+1)
        //Aの係数と、定数の、最大公約数をユークリッドの互助法で求める

        long Keisuu = N;

        //等差数列の和の公式で1からN-1までの和を求める
        long Makkou = N - 1;
        long Teisuu = Makkou * (Makkou + 1) / 2;

        Console.WriteLine(DeriveGCD(Keisuu, Teisuu));
    }

    //ユークリッドの互除法で2数の最大公約数を求める
    static long DeriveGCD(long pVal1, long pVal2)
    {
        long WarareruKazu = pVal2;
        long WaruKazu = pVal1;

        while (true) {
            long Amari = WarareruKazu % WaruKazu;
            if (Amari == 0) return WaruKazu;
            WarareruKazu = WaruKazu;
            WaruKazu = Amari;
        }
    }
}
0