HDU 2058 The sum problem(枚举)
2016-02-18 12:38
295 查看
The sum problem
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
20 10
50 30
0 0
[align=left]Sample Output[/align]
[1,4]
[10,10]
[4,8]
[6,9]
[9,11]
[30,30]
[align=left]Sample Input[/align]
本来想前缀和搞搞的,一看1 <= N, M <= 1000000000,看来是公式题,给出一个M,不可能从1枚举到1000000000,所以要找一个枚举范围,等差数列求和公式:sum=n*a1+n*(n-1)/2,n要最大,就要a1=1,所以sum=n*(n+1)/2,变一下n<sqrt(2*sum),所以取n=sqrt(2*sum),接下来n从大到小枚举,用公式变形:a1=sum/n-(n-1)/2求出a1,求和如果等于M,输出[a1,a1+n-1].
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); //freopen("out.txt","w",stdout); #endif // LOCAL ios::sync_with_stdio(false); int n,m; while(cin>>n>>m,n||m) { int t=sqrt(2*m); while(t) { int ans=m/t-(t-1)/2; if(ans*t+(t*(t-1)/2)==m) printf("[%d,%d]\n",ans,ans+t-1); t--; } printf("\n"); } return 0; }
View Code
相关文章推荐
- SQLAlchemy 简单笔记
- 网络工程
- 文章标题
- UIImagePickerController(相册的使用)
- 2.0.3 新浪的接口文档的规范
- WordPress
- GNU/Linux
- Android Canvas绘图详解(图文)
- Linux
- STM32F030C8T6 STM32F030R8T6 STM32F030 STOP低功耗模式配置 11uA
- iOS 图片扫描(Moodstocks)
- [golang]反射的用处--代码自动生成
- Git常用命令收集
- google输入法 日文 [CapsLock]キー ひらがな⇔直接入力の設定
- #android#android studio 使用cardview
- luajit stack traceback() 显示不全
- BestCoder #69 Baby Ming and phone number(HDOJ5611)
- Date and Time
- 怎样在win7中 安装Tomcat7.0
- DOM