HDU

题目链接:https://vjudge.net/problem/HDU-2058

题目大意:给你1~n的所有数,问你所有的和为m的连续序列

  因为n,m比较大,所以尺取肯定是不行的,因为是自然数组成的序列,所以可以用等差数列求和公式,因为d为1,所以公式可以写成(2*a1 + k-1)*k/2 = m,变形过后的结果是a1 = 2*m+k-k*k/2k,an = a1 + k - 1,这时候我们只要枚举k就能得到a1,an,那么k枚举到多少呢?右边公式再变形一下得到a1 = m/k + 1/2 - k/2,显然这是一个单调递减的函数,当k*k = 2m时,a1 = 1/2,这个时候a1已经比1小了。说明前面已经枚举出所有结果了,我们我们只要将k从1枚举到sqrt(2*m)即可

#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '
'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e5+10;
int arr[maxn];
int main(void) {
    ll n, m;
    while(~scanf("%lld%lld", &n, &m) && (n || m)) {
        ll st = sqrt(2*m)+eps;
        for (ll i = st; i>=1; --i) {
            ll u = 2*m+i-i*i, d = 2*i;
            if (!(u%d)) printf("[%lld,%lld]
", u/d, u/d + i-1);
        }
        putchar(endl);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shuitiangong/p/12441378.html