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.

InputInput contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0. 
OutputFor 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<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<math.h>
using namespace std;
/*
思路:
区间项数为i
起始位置 a,终点为b则b=a+i-1;
这一段区间的和为sum=(a+b)*i/2=(a+a+i-1)*i/2=m;
a最小为1,则(1+1+i-1)*i/2<=m即(i+1)*i<=2m , i有一定小于sqrt(2m)
即 0<i<sqrt(2m);判断条件就是 (a+a+i-1)*i==2*m;
*/
int main()
{
int n,m,a,b;
while(cin>>n>>m)
{
if(n== && m==)
break;
for(int i=sqrt(*m);i>=;i--)//最小为1项,就是[m,m]
{
a=m/i-(i-)/;//等差公式 即 m=a*i+i*(i-1)/2-----m/i=a+(i-1)/2---a=m/i-(i-1)/2;
if((a+a+i-)*i==*m)
{
printf("[%d,%d]\n",a,a+i-);
}
}
printf("\n");
}
return ;
}

J - The sum problem的更多相关文章

  1. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  3. 动态规划法(三)子集和问题(Subset sum problem)

      继续讲故事~~   上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界.   这天,丁丁刚回到家,他 ...

  4. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  5. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  6. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  8. NYOJ--927--dfs--The partial sum problem

    /* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...

  9. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. ajax结合sweetalert弹出框删除数据

    思路:

  2. golang工具之present - 编写go特色的ppt

    简介: Golang Present 是 Golang 社群开发出來的一个简单工具,通过简单的语法可以制作 ppt(语法近似于 Markdown).   默认文件格式是 .slide ,是随着 gol ...

  3. 基础组合问题 ————从n个物品里选m个

    package test; import java.util.*; public class Main{ public static int f(int n,int k, int goal){ if( ...

  4. javascript正则表达式入门先了解这些

    前言 此内容由学习<JavaScript正则表达式迷你书(1.1版)>整理而来(于2020年3月30日看完).此外还参考了MDN上关于Regex和String的相关内容,还有ECMAScr ...

  5. mysql5.6配置文件详解(一)

    mysqld  Ver 5.6.11 for Linux on x86_64 (Source distribution)Copyright (c) 2000, 2013, Oracle and/or ...

  6. R语言基本操作

    is.na and is.element is.na can use which, it finds specific rows, is.element can't, it is designed t ...

  7. 使用 keras 和 tfjs 构建血细胞分类模型

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识!

  8. Google浏览器截取整个网页

    ~Ctrl+shift+i(开发者工具) ~Ctrl+shift+p ~输入full  

  9. Html,css构建一个对话框,练习201911281028

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  10. 三角函数在Three.js中的点的移动轨迹应用

    在学习2D文字的时候,看到官网有这样一个示例: https://threejs.org/examples/#css2d_label ![](https://img2018.cnblogs.com/bl ...