AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC390-D Stone XOR
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("2 5 7");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("2");
WillReturn.Add("100000000000000000 100000000000000000");
//2
}
else if (InputPattern == "Input3") {
WillReturn.Add("6");
WillReturn.Add("71 74 45 34 31 60");
//84
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
int UB = AArr.GetUpperBound(0);
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.ValList = new List<long>();
WillPush.CurrInd = 0;
Stk.Push(WillPush);
var AnswerList = new List<long>();
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
int CurrInd = Popped.CurrInd;
if (CurrInd > UB) {
long XOR = 0;
Popped.ValList.ForEach(pX => XOR ^= pX);
AnswerList.Add(XOR);
continue;
}
// Listの要素に足し算する場合
for (int I = 0; I <= Popped.ValList.Count - 1; I++) {
WillPush.ValList = new List<long>(Popped.ValList);
WillPush.ValList[I] = Popped.ValList[I] + AArr[CurrInd];
WillPush.CurrInd = CurrInd + 1;
Stk.Push(WillPush);
}
// ListにAddする場合
WillPush.ValList = new List<long>(Popped.ValList);
WillPush.ValList.Add(AArr[CurrInd]);
WillPush.CurrInd = CurrInd + 1;
Stk.Push(WillPush);
}
// ソートして前と不一致かで、uniq件数を調べる
long Answer = 0;
AnswerList.Sort();
for (int I = 0; I <= AnswerList.Count - 1; I++) {
if (I == 0) {
Answer++;
continue;
}
if (AnswerList[I] != AnswerList[I - 1]) {
Answer++;
}
}
Console.WriteLine(Answer);
}
struct JyoutaiDef
{
internal List<long> ValList;
internal int CurrInd;
}
}
解説
ベル数の12は、
4213597なのでDFSで全探索してます。
重複チェックをHashSetでやるとTLEするので
ソートして前後との差異を見てます。