51Nod 1138连续整数的和

传送门

解析:

第一眼看到题目说剪枝,于是写了个超时的DFS上去结果TLE。后来看博客发现,根据等差数列的求和公式,设len为子数列的长度,为子数列首项,则有 len * (2 * t + len - 1)/ 2 = sum

如果子数列从1开始,显然该子数列为同等长度下子数列的最小值。此时,sum * 2 = len * (len + 1)故有:len <= sqrt(sum * 2)

即,要找的子数列长度不会超过sqrt(2*n)。

所以我们就可以从len = sqrt(2*n 开始,枚举并验证有没有符合公式的整数作为首项

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
 
const int MAXN = 50005;
 
int main() {
	int n;
	bool flag = false;
	cin >> n;

	int len = sqrt(1LL * 2 * n);

	for(int i=len; i>1; --i){
		double a = ( 2.0 * n/ i - i + 1) / 2.0;
		if( a == (int) a){
			flag = true;
			cout << (int)a << endl;
		}
	}

	if(!flag)
		cout << "No Solution";
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值