HDU 2058 The sum problem
2016-01-28 16:39
302 查看
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20506 Accepted Submission(s): 6023 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] Author 8600 Source 校庆杯Warm Up |
Sn=(a1+an)*n/2
=(a1+a1+d(n-1))*n/2
=a1*n+d(n-1)*n/2;
因为此处公差d=1,所以Sn=a1*n+(n-1)*n/2,从第一项开始算起时(本题a1=1时),可化简为Sn=n+(n-1)*n/2=(n+1)n/2;
m=Sn,n为项的个数,则len<=n(max)=sqrt(Sn*2)=sqrt(m*2);
m=Sn =a1*n+(n-1)n/2可得a1*n=m-(n-1)n/2;
如果得到的a1*n这个数能否被len整除,则Sn=m
#include <iostream> #include <cstdio> #include <cmath> #define ll long long using namespace std; int main() { long long n,m; while(cin>>n>>m&&n&&m){ int len=(int)sqrt(m*2.0); int l=0;//a1和len的乘积 for(;len>0;len--){ l=m-(len-1)*len/2;//a1*n=m-(n+1)*n/2 if(l%len==0) cout<<"["<<l/len<<","<<l/len+len-1<<"]"<<endl; } cout<<endl; } return 0; }
相关文章推荐
- 第二次作业———“A+B Format”思路与总结
- springMvc integrated with springSecurity 常见问题
- 数据恢复流程
- Oracle创建表空间及在指定的表空间上创建数据库的步骤
- python下的tcp,udp服务器端,客户机端
- 生产数据库的安全管理“白名单”
- new的返回值检查
- Android中如何解析JSON数据
- route add 没文化真可怕
- 电缆线径的选择
- Xcode配置OpenGL
- UITableView中Cell重用机制导致内容重复解决方法
- 飞控控制方法学习
- 数列分段
- 【spring mvc (三)】spring 容器
- Android中如何解析XML数据
- mongodb学习1(基础知识)
- 使用fragment,editvView ,listView,出现软键盘遮挡
- AES SHA-1 SM3 MD5
- 记某图片下载器破解笔记之增加试用次数