AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC134-D Preparing Boxes
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 0 0");
//1
//1
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("0 0 0 0 0");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
var AList = new List<int>();
AList.Add(0);
AList.AddRange(AArr);
int UB = AList.Count - 1;
int[] AnswerArr = new int[UB + 1];
// 逆から見ていく
for (int I = UB; 1 <= I; I--) {
// 倍数の位置の、ボールの個数合計を求める
int BallCnt = 0;
for (int J = I; J <= UB; J += I) {
BallCnt += AnswerArr[J];
}
if (BallCnt % 2 != AList[I]) {
AnswerArr[I] = 1;
}
}
var AnswerIndList = new List<int>();
for (int I = 1; I <= UB; I++) {
if (AnswerArr[I] == 1) {
AnswerIndList.Add(I);
}
}
Console.WriteLine(AnswerIndList.Count);
if (AnswerIndList.Count > 0) {
string[] AnswerStrArr = Array.ConvertAll(AnswerIndList.ToArray(), pX => pX.ToString());
Console.WriteLine(string.Join(" ", AnswerStrArr));
}
}
}
解説
箱を逆から見ていき、
ボールの有無を決めてます。