HDU - 2058 The sum problem(思路题)

题目:

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]\n",a1,a1+i-1);
            }
        }
        puts("");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/sykline/p/10466594.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值