AtCoderのAGC    次のAGCの問題へ    前のAGCの問題へ

AGC024-A Fairness


問題へのリンク


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("1 2 3 1");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 3 2 0");
            //-1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1000000000 1000000000 1000000000 1000000000000000000");
            //0
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        // Simulation();

        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long A = wkArr[0];
        long B = wkArr[1];
        long C = wkArr[2];
        long K = wkArr[3];

        long Answer = SolveNaive(A, B, C, K % 2);
        Console.WriteLine(Answer);
    }

    static void Simulation()
    {
        var InsRandom = new Random();

        for (int I = 1; I <= 30; I++) {
            long A = InsRandom.Next(1, 10);
            long B = InsRandom.Next(1, 10);
            long C = InsRandom.Next(1, 10);
            long K = InsRandom.Next(0, 10);

            Console.WriteLine("■■■■■");
            for (int LoopK = 0; LoopK <= K; LoopK++) {
                long Answer = SolveNaive(A, B, C, LoopK);
                Console.WriteLine("A={0},B={1},C={2},K={3}だと{4}", A, B, C, LoopK, Answer);
            }
        }
    }

    static long SolveNaive(long pA, long pB, long pC, long pK)
    {
        long[] PrevArr = new long[3];
        PrevArr[0] = pA;
        PrevArr[1] = pB;
        PrevArr[2] = pC;
        for (long I = 1; I <= pK; I++) {
            long[] CurrArr = (long[])PrevArr.Clone();
            CurrArr[0] = PrevArr[1] + PrevArr[2];
            CurrArr[1] = PrevArr[0] + PrevArr[2];
            CurrArr[2] = PrevArr[0] + PrevArr[1];

            PrevArr = CurrArr;
        }
        return PrevArr[0] - PrevArr[1];
    }
}


解説

乱数でシュミレーションすると、
2が周期と分かるので、
KをK%2に変更して、ナイーブに解いてます。