AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC177-C Sum of product of pairs
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("3");
WillReturn.Add("1 2 3");
//11
}
else if (InputPattern == "Input2") {
WillReturn.Add("4");
WillReturn.Add("141421356 17320508 22360679 244949");
//437235829
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long ModVal = 1000000000 + 7;
long[] AArr = InputList[1].Split(' ').Select(X => long.Parse(X)).ToArray();
long wkSum = 0;
foreach (long EachInt in AArr) {
wkSum += EachInt;
wkSum %= ModVal;
}
long Answer = 0;
for (long I = 0; I <= AArr.GetUpperBound(0) - 1; I++) {
wkSum -= AArr[I];
wkSum += ModVal;
wkSum %= ModVal;
Answer += AArr[I] * wkSum;
Answer %= ModVal;
}
Console.WriteLine(Answer);
}
}
解説
A,B,C,Dの4つとしたら
Answer = A*B + A*C + A*D + B*C + B*D + C*D となり、変形すると
Answer = A*(B+C+D) + B*(C+D) + C*D なので、
最初に、A+B+C+Dの値を計算してます。