1120. Sum of Sequential Numbers

本文介绍了一种用于解决序列求和问题的高效算法,通过数学推导和编程实现,展示了如何在限定时间内找到最大可能的序列长度,使得序列之和等于给定数。该算法特别适用于需要快速计算大量数据场景。

1120. Sum of Sequential Numbers

Time Limit: 1.0 second
Memory Limit: 16 MB
There is no involute formulation concerning factitiously activity of SKB Kontur in this problem. Moreover, there is no formulation at all.

Input

There is the only number N, 1 ≤ N ≤ 109.

Output

Your program is to output two positive integers A and P separated with a space such that:
  1. N = A + (A + 1) + … + (A + P − 1).
  2. You are to choose a pair with the maximal possible value of P.

Sample

input output
14
2 4


剪一下枝就可以了~

#include <iostream>
#include <cmath>
using namespace std;

bool temp(int p, int n)
{
	if ((n*2) % p != 0)
		return 0;
	int a = (n*2) / p + 1 - p;
	if (a <= 0 || (a&1))
		return 0;
	return 1;
}

int main()
{
	int n, p;
	cin>>n;
	p = sqrt(n*2.0);
	while (!temp(p, n))
		p--;
	cout<<(n*2/p + 1-p)/2<<" "<<p<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值