using System; using System.Collections.Generic; using System.Linq; class Program { class RangeInfo { public int Start {get; private set;} public int Last {get; private set;} public int Length { get { return Last - Start + 1; } } public RangeInfo(int start, int last) { this.Start = start; this.Last = last; } } static IEnumerable<RangeInfo> Solve726(int n) { int last = (int)(-1 + Math.Sqrt(1.0 + 8 * n) / 2); int first = 0; int d = last * (last + 1) / 2 - n; while (first <= last) { switch (Math.Sign(d)) { case -1: d += ++last; break; case 1: d -= first++; break; default: yield return new RangeInfo(first, last); d -= first++; break; } } } static void Main(string[] args) { int N = 2014; foreach (var x in Solve726(N)) { var r = Enumerable.Range(x.Start, x.Length).Select(n => n.ToString()); Console.WriteLine(string.Join(",", r.ToArray())); } } }
Standard input is empty
12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64 97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115 502,503,504,505 2014