hdu2058 The sum problem(枚举~~等差数列求和公式)

The sum problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17697    Accepted Submission(s):
5275

Problem Description

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.

Input

Input contains multiple test cases. each case contains
two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M =
0.

Output

For each test case, print all the possible sub-sequence
that its sum is M.The format is show in the sample below.print a blank line
after each test case.

Sample Input

20 10
50 30
0 0

Sample Output

[1,4]
[10,10]

[4,8]
[6,9]
[9,11]
[30,30]

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxn 1000000
__int64 N,M;
int main()
{
    while(scanf("%I64d%I64d",&N,&M)!=EOF&&N&&M)
    {
        for(__int64 k=(int)sqrt(2*M);k>=1;k--)
        {
            __int64 a1=M/k-(k-1)/2;
            if((2*a1+k-1)*k==2*M)
            {
                printf("[%I64d,%I64d]\n",a1,a1+k-1);
            }
        }
        printf("\n");
    }
    return 0;
}
//根据等差数列求和,m=(2*a1+k-1)*k/2,k表示数列的项数,a1表示首项。
//枚举k(1<=k<=sqtrt(m)) 
时间: 2024-12-13 04:18:09

hdu2058 The sum problem(枚举~~等差数列求和公式)的相关文章

The sum problem

The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16197 Accepted Submission(s): 4843 Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub

HDU-2058-The sum problem(数学题技巧型)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2058 思路: 这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式, (1)sn=n*(a1+an)/2: 即可代入公式,(2)m=(e-s+1)*(s+e)/2                         注释*******//s代表起点,e代表终点. 则由(2)推出,(3)e=(int)(2*m+s*s-s),根据e点找s点,代入(1)成立则输出[s,e]; 

HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 题意是输入一个N,求N被分成1个数的结果+被分成2个数的结果+...+被分成N个数的结果,N很大 1.隔板原理 1~N有

HDU 1001 Sum Problem

Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. InputThe input will consist of a series of integers n, one integer per line. OutputFor each ca

[C/E] 等差数列求和

题目:要求给定一个整数 N,求从 0 到 N 之间所有整数相加之和. 解1:使用 for 循环依次递加. #include <stdio.h> int main(void){ int x; printf("Input an integer:\n"); scanf("%d", &x); printf("sum=%d\n", sum(x)); return 0; }; int sum(int x){ int i, result=0

等差数列求和模板

一直就没彻底搞清楚这个问题. 在这里总结下. 一.1+2+3+4+...+n 这个公式还是记得住的:n*(n+1)/2 (编程的时候顺序不能变) 二.起始项为a1,终止项为an,总共有n项 这时候分情况讨论 1. n%2 == 0 则输出 (a1+an)*(n/2) 2. n%2 == 1 定理:当n%2==1时,(a1+an)%2 == 0 证明: an = a1 + (n-1)*d a1+an = 2*a1+(n-1)*d 所以(a1+an)%2 == 0 有了这个定理,于是输出((a1+a

HDU 2058 The sum problem (数学+暴力)

题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能不能优化一下,找一个范围.想到这是一个连续的序列而且是从1开始的,这不就是一个等差数列么,公差是1罢了.由求和公式得Sn = (a1+an) * n / 2;所以说n最大就是sqrt(M*2)(想一想为什么),因为a1+an 一定是大于n的.如果我们取区间的和,那么Sn = (ai+aj) * (j

Maxmum subsequence sum problem

We have a lot of ways to solve the maximum subsequence sum problem, but different ways take different time. 1.Brute-force algorithm int maxSubSum1(const vector<int> &a) { int maxSum=0; for(int i=0;i<a.size();i++) for(int j=i;j<a.size();j++

hdu 2576 Another Sum Problem

题目大意:求前n项和的前n项和. 数学推导题,f(n)=n*(n+1)*(n+2)/6 推导思路如下: #include"cstdio" #include"cstring" #include"cmath" #include"cstdlib" #include"iostream" #include"algorithm" #include"queue" using nam