AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC164-D Multiple of 2019
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("1817181712114");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("14282668646");
//2
}
else if (InputPattern == "Input3") {
WillReturn.Add("2119");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
string S = InputList[0];
int UB = 2018;
// 場合の数[mod 2019]なDP表
int[] PrevDP = new int[UB + 1];
long Answer = 0;
for (int I = 0; I <= S.Length - 1; I++) {
int[] CurrDP = new int[UB + 1];
int CurrNum = S[I] - '0';
CurrDP[CurrNum]++;
for (int J = 0; J <= UB; J++) {
if (PrevDP[J] == 0) continue;
int NewInd = J * 10 + CurrNum;
NewInd %= 2019;
CurrDP[NewInd] += PrevDP[J];
}
// 解を計上
Answer += CurrDP[0];
PrevDP = CurrDP;
}
Console.WriteLine(Answer);
}
}
解説
解を計上していくタイプの動的計画法で解いてます。