AtCoderのARC
次のARCの問題へ
前のARCの問題へ
ARC112-A B = C
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("5");
WillReturn.Add("2 6");
WillReturn.Add("0 0");
WillReturn.Add("1000000 1000000");
WillReturn.Add("12345 67890");
WillReturn.Add("0 1000000");
//6
//1
//0
//933184801
//500001500001
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
for (int I = 0; I <= 15; I++) {
for (int J = I; J <= 15; J++) {
DeriveCnt(I, J);
}
}
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
long L = wkArr[0];
long R = wkArr[1];
long N = R - 2 * L + 1;
if (N < 1) {
Console.WriteLine(0);
}
else {
Console.WriteLine(N * (N + 1) / 2);
}
}
}
static void DeriveCnt(long pL, long pR)
{
long Answer = 0;
for (long LoopA = pL; LoopA <= pR; LoopA++) {
for (long LoopB = pL; LoopB <= pR; LoopB++) {
for (long LoopC = pL; LoopC <= pR; LoopC++) {
if (LoopA == LoopB + LoopC) {
Answer++;
}
}
}
}
Console.WriteLine("DeriveCnt({0},{1})={2}", pL, pR, Answer);
}
}
解説
15以下の組合せで実験すれば、三角数になると分かるので
三角数の一般項である、等差数列の和の公式を使ってます。