using System; using System.Collections; using System.Collections.Generic; namespace Brux { /// /// A323452 First differences of A323289. /// class Program { static void Main(string[] args) { List gen = new List(); gen.Add(1); foreach (long v in gen) { Visit(v); } for (int n = 1; n <= 20; n++) { List newgen = new List(); foreach (long v in gen) { newgen.AddRange(Others(v)); } gen = newgen; Console.WriteLine("{0} {1}", n, gen.Count); } } static IEnumerable Others(long n) { foreach (long o in Move(n)) { if (!Visited(o)) { Visit(o); yield return o; } } } static IEnumerable Move(long v) { string n = v.ToString(); for (int w = 1; w <= n.Length; w++) { for (int o = 0; o <= n.Length - w; o++) { if (n[o] != '0') { string head = n.Substring(0, o); long middle = long.Parse(n.Substring(o, w)); string tail = n.Substring(o + w); yield return long.Parse(string.Format("{0}{1}{2}", head, middle * 2, tail)); if (middle % 2 == 0) { yield return long.Parse(string.Format("{0}{1}{2}", head, middle / 2, tail)); } } } } } static bool Visited(long v) { if (v < limit) { return small.Get((int)v); } else { return big.Contains(v); } } static void Visit(long v) { if (v < limit) { small.Set((int)v, true); } else { big.Add(v); } } const long limit = 1000000000; static readonly BitArray small = new BitArray((int)limit); static readonly HashSet big = new HashSet(); } }