AtCoderのARC
次のARCの問題へ
前のARCの問題へ
ARC110-A Redundant Redundancy
C#のソース
using System;
using System.Collections.Generic;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
//7
}
else if (InputPattern == "Input2") {
WillReturn.Add("10");
//39916801
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int N = int.Parse(InputList[0]);
var NumList = new List<int>();
for (int I = 2; I <= N; I++) {
NumList.Add(I);
}
long CurrLCM = NumList[0];
for (int I = 1; I <= NumList.Count - 1; I++) {
long CurrGCD = DeriveGCD(CurrLCM, NumList[I]);
CurrLCM = CurrLCM * NumList[I] / CurrGCD;
}
Console.WriteLine(CurrLCM + 1);
}
// ユークリッドの互除法で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;
}
}
}
解説
最小公倍数+1が解なので
最小公倍数を求めてます。