AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC253-E Distance Sequence
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("2 3 1");
//6
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 3 2");
//2
}
else if (InputPattern == "Input3") {
WillReturn.Add("100 1000 500");
//657064711
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
const long Hou = 998244353;
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long N = wkArr[0];
long M = wkArr[1];
long K = wkArr[2];
long UB = M;
// 場合の数[最終値]なDP表
long[] PrevDP = new long[UB + 1];
PrevDP[1] = 1;
for (long I = 2; I <= N; I++) {
long[] CurrDP = new long[UB + 1];
long RunSum = 0;
for (long J = 0; J <= UB; J++) {
RunSum += PrevDP[J];
RunSum %= Hou;
long ImosEnd1 = J - K + 1;
long ImosSta2 = J + K;
if (1 <= ImosEnd1 && ImosEnd1 <= UB) {
CurrDP[1] += RunSum;
CurrDP[1] %= Hou;
CurrDP[ImosEnd1] -= RunSum;
if (CurrDP[ImosEnd1] < 0) {
CurrDP[ImosEnd1] += Hou;
}
}
if (ImosSta2 <= UB) {
CurrDP[ImosSta2] += RunSum;
CurrDP[ImosSta2] %= Hou;
}
}
PrevDP = CurrDP;
}
long Answer = 0;
long RunSum2 = 0;
for (long J = 0; J <= UB; J++) {
RunSum2 += PrevDP[J];
RunSum2 %= Hou;
Answer += RunSum2;
Answer %= Hou;
}
Console.WriteLine(Answer);
}
}
解説
配るDPで解いてます。
配る先が連続した区間なので、いもす法で配ってます。