51nod 1138 连续整数的和

来源:互联网 发布:化学编辑软件 编辑:程序博客网 时间:2024/06/05 06:53

等差数列前n项和公式:S = na1+n(n-1)/2,a1 = (S-n(n-1)/2)/n,可以求出n的范围大概就是[2,sqrt(2*S)],枚举序列长度n,然后求解a1。

using System;using System.IO;using System.Numerics;namespace timeless{    class Program    {        static void Main(string[] args)        {            StreamReader sr = new StreamReader(Console.OpenStandardInput());            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());            int n = Convert.ToInt32(sr.ReadLine());            int len = (int)Math.Sqrt(n*2);            bool flag = false;            for (int i = len; i >= 2; --i)            {                if ((n - (i * (i - 1)) / 2) % i == 0)                {                    flag = true;                    sw.WriteLine((n - (i * (i - 1)) / 2)/ i);                }            }            if (!flag)                sw.WriteLine("No Solution");            sw.Flush();            sw.Close();            sr.Close();        }    }}