HD2058The sum problem
The sum problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12422 Accepted Submission(s): 3757
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] 这也是一道水题,不过一开始没看懂题意,后面读了几遍就懂了。 题目给两个数N,M,要求找出从1开始到N里的子序列的和是M,并全部输出,一开始可能会想到求和公式,首项加末项乘以项数除以二,公式里有两个变量,题目告知M可以是一个很大的数,如果利用公式直接穷举去遍历所有的数的话的话会溢出,所以要换个角度去想。 考虑子串的长度为j,首项为i的数列满足要求,则(i+i+j-1)*j/2=m,==>(2*i-1+j)*j=2m,其中2*i-1>0; 所以j*j<2*m;j<sqrt(2*m);这样确定子串的长度了; 利用j和m,求出i的表达式,接下来就很简单了:#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n,m,i;
while(cin>>n>>m && !(n==0 && m==0)){
int j = sqrt((double)2*m);
for(j;j>=1;j--){ //去掉douubl会在提交时显示编译错误,要声明函数重载时的类型
i =(2*m/j+1-j)/2;
if((2*i-1+j)*j/2==m)
cout<<"["<<i<<","<<i+j-1<<"]"<<endl;
}
cout<<endl;
}
return 0;
}
HD2058The sum problem的更多相关文章
- 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 ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- 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 ...
- Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- 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 ...
- 动态规划法(三)子集和问题(Subset sum problem)
继续讲故事~~ 上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界. 这天,丁丁刚回到家,他 ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Problem-1001:Sum Problem
Sum Problem Sample code : #include <stdio.h> int main() { int i,n; int sum; while(scanf(" ...
随机推荐
- 实现图片大小的自动控制( 图片大小控制CSS代码)
图片大小控制CSS代码 将以下代码放到你的样式表文件中即可实现图片大小的自动控制. /*图片大小控制CSS By Tekin */img,a img{border:0;margin:0;padding ...
- dom4j创建格式化的xml文件
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java. ...
- LA 3516 Exploring Pyramids (记忆化搜索)
题意 给定一个DFS序列,问能有多少树与之对应. 思路 设输入序列为S,dp(i, j)为子序列Si, Si+1, --, Sj对应的树的个数,则边界条件为d(i, i) = 1,且Si != Sj时 ...
- Fragment的知识总结
1. Fragment概念及作用. 以下是使用Fragment提供思路 2. 创建继承于 Fragment的类:(可extends Fagment 或 ListFagment) 注意导包:如果考虑兼 ...
- putty保持Session链接不断开的方法
利用Putty登陆到远程主机后,如果长时间没有做任何操作,服务器会与本地客户端断开连接 假如设置了会话连接功能,就会每隔多少秒,客户端会发送一个空数据包给服务器,保持连接. 1. 打开putty.ex ...
- 【转】Eclipse编辑shell的插件(shellEd)
原文网址:http://blog.sina.com.cn/s/blog_a42d507e01019mlp.html Eclipse官方网站:http://download.eclipse.org/ 1 ...
- Hadoop学习总结之二:HDFS读写过程解析
一.文件的打开 1.1.客户端 HDFS打开一个文件,需要在客户端调用DistributedFileSystem.open(Path f, int bufferSize),其实现为: public F ...
- jdbc操作数据库返回结果集的注意事项
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- Android彩蛋效果,微信彩蛋效果
根据Android源码修改,具有微信彩蛋效果 主要代码 public static class Board extends FrameLayout { public static final bool ...
- npoi z
http://blog.csdn.net/fireghost57/article/details/25623143 http://www.cnblogs.com/jiagoushi/archive/2 ...