您的位置:首页 > 其它

51nod 1138 连续整数的和

2017-07-24 15:17 211 查看
等差数列前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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: