解析:
第一眼看到题目说剪枝,于是写了个超时的DFS上去结果TLE。后来看博客发现,根据等差数列的求和公式,设len为子数列的长度,t 为子数列首项,则有 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;
}