AtCoderの企業コンテスト
次の企業コンテストの問題へ
前の企業コンテストの問題へ
SoundHound Programming Contest2018 C Ordinary Beauty
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");
//1.0000000000
}
else if (InputPattern == "Input2") {
WillReturn.Add("1000000000 180707 0");
//0.0001807060
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
decimal[] wkArr = InputList[0].Split(' ').Select(pX => decimal.Parse(pX)).ToArray();
decimal n = wkArr[0];
decimal m = wkArr[1];
decimal d = wkArr[2];
decimal Answer;
if (d == 0) {
Answer = (m - 1) / n;
}
else {
Answer = (m - 1) * (n - d) * 2 / (n * n);
}
Console.WriteLine(Answer);
}
}
解説
30 100 5
という入力を考えます。
項差は全部で99個ありありますが
1項目と2項目の項差が5になるのは、
(1,6)
(2,7)
(3,8)
・
・
・
(25,30)
で、25通りあり、1項目と2項目を入れ替えもあるので50通りあります。
残りの項は自由に設定できるので、これは
mの(n-2)乗通りあります。
全部で項差は99個あり、数列はnのm乗通りあるので
解は
(m - 1) * (n - d ) * 2 * nの(m-2)乗
-----------------------------------
nのm乗
で、約分すると
(m - 1) * (n - d ) * 2
-----------------------
nの2乗
になります。
dが0の場合は、入れ替えのペアが無いですが、同様に式変形すると
(m - 1) / n
が解になります。