hdu 2058 The sum problem
来源:互联网 发布:简谱制作软件app 编辑:程序博客网 时间:2024/05/22 13:29
链接:http://hdu.hustoj.com/showproblem.php?pid=2058
The sum problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23527 Accepted Submission(s): 6998
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 1050 300 0
Sample Output
[1,4][10,10][4,8][6,9][9,11][30,30]
Author
8600看着很简单的题,求1-n中连续的几个和是m的数
错误原因:超时
也想过(首相+末项)*项数/2==m,但是推出来还是要两个for循环,嗖的就超时了,看了discuss,没太看明白,有知道的dalao可以给讲讲,推公式真的不在行
暂且放其他人的代码和题解:
1.
//等差数列求前n项和————Sn=na1+((n-1)*n*d)/2#include<iostream>#include<cmath>using namespace std;int main(){ long long int m,n,a; while(cin>>n>>m) { if(n==0&&m==0) break; for(int i=sqrt(2*m);i>0;i--)//保证a1的值大于0 { a=m/i-(i-1)/2; if((2*a+i-1)*i==2*m) cout<<"["<<a<<","<<a+i-1<<"]"<<endl; } cout<<endl; } return 0;}
2.
((首项+末项)*项数)/2=m以及 末项=首项+项数-1联立方程组可以得到一个关于左区间项,项数,M的一个方程。(高中课程,不详细展开了)假设首项是1,我们代入,很容易得到n(n+1)=2m这个公式(n是项数)。 这里可以把n+1看成n,n^2=2m,n=sqrt(2m); 也就是说项数最多的情况也只有sqrt(2m)。这也就是说,假设M给的是10的9次方,最大的答案区间撑死也只有46000左右的长度。也就是2的10次的算术平方根那么我们枚举1---sqrt(2m),假设是长度代入到公式中。m知道了,长度知道了,那么可以解出首项,我们知道,这个数列的每一项必定是个正整数,那么如果算出来的首项正好是个正整数,那么就是个答案,区间就是【我们算出来的整数,整数+长度-1】输出即可。(我是不是傻了,没怎么看懂)
代码:
#define _CRT_SBCURE_MO_DEPRECATE #include<iostream> 1#include<stdlib.h> #include<stdio.h> #include<cmath> #include<algorithm> #include<string> #include<string.h> #include<set> #include<queue> #include<stack> #include<functional> using namespace std;const int maxn = 1000000 + 10;const int maxm = 10000 + 10;const int INF = 0x3f3f3f3f;int n, m;int a, len;double x;int main(){while (scanf("%d %d", &n, &m) != EOF && n && m) {len = floor(sqrt(2 * m));for (int i = len; i >= 1; i--) {x = 1.0*(2 * m) / i + 1 - i; x = 0.5*x;a = floor(x);//向下取整x = x - a;if (x == 0 && a <= n && (a + i - 1) <= n) { printf("[%d,%d]\n", a, a + i - 1); }}printf("\n");}system("pause");return 0;}
0 0
- hdu 2058 The sum problem
- hdu 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- HDU 2058 The sum problem
- hdu---2058The sum problem
- The sum problem hdu 2058
- hdu 2058 The sum problem
- HDU 2058 The Sum Problem
- HDU-2058 The sum problem
- hdu 2058 The sum problem
- HDU 2058 The sum problem
- hdu 2058 The sum problem
- The sum problem hdu 2058
- ssh连接远程主机执行脚本的环境变量问题
- H.266:帧内角度预测
- 剑指Offer——中国银行面试知识储备+面试内容(Offer-2已收割)
- 了解Binder机制(极简主义解释风格)
- 错题库
- hdu 2058 The sum problem
- 统计进入公园的人总数
- 手把手叫你制作一个精美的在线音乐播放器
- Qt中model/view对数据列排序
- 基于Qt5.6做一个最简单的具有收发功能的串口助手
- CodeForces 586D Phillip and Trains(搜索)
- 如何搭建自己域名和服务器的博客
- Leetcode 462. Minimum Moves to Equal Array Elements II 使数组相等 解题报告
- ACM中浮点数精度问题