HDU

题目:

Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.

思路:

刚开始做写了一个尺取法,脑抽的一批。(这几天心情真的是颓的很,连带脑瓜也不好用了,抓紧调整~~~~)

其实可以根据等差数列求和公式求出这个子序列的长度的一个大致的范围为k=sqrt(2*m),然后根据m和k求出首项a1,然后将a1、k和m待会原来的式子看看等号是不是成立。

如果成立就打印区间[a1,a1+k-1]

代码:

#include <bits/stdc++.h>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000000
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<char,char> P;
const int maxn = 1000005;
ll m,n;

int main() {
    while(scanf("%lld%lld",&n,&m) && n) {
        ll t = sqrt(2*m),a1;
        for(int i=t; i>=1; i--) {
            a1 = (2*m-i*i+i)/(2*i);
            if(i*i+(2*a1-1)*i==2*m) {
                printf("[%lld,%lld]
",a1,a1+i-1);
            }
        }
        puts("");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sykline/p/10466594.html