AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC174-C Repsept
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("101");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("2");
//-1
}
else if (InputPattern == "Input3") {
WillReturn.Add("999983");
//999982
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long K = long.Parse(InputList[0]);
// 初項は7で、漸化式は10倍して7を足す
long CurrVal = 7 % K;
long MaxKouNo = 1;
var AppearValSet = new HashSet<long>();
while (true) {
if (CurrVal == 0) {
Console.WriteLine(MaxKouNo);
return;
}
CurrVal = CurrVal * 10 + 7;
CurrVal %= K;
if (AppearValSet.Add(CurrVal) == false) {
break;
}
MaxKouNo++;
}
Console.WriteLine(-1);
}
}
解説
漸化式を求めてから、mod Kで考えてます。