HDU 2058:The sum problem(数学)
The sum problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31453 Accepted Submission(s): 9414
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]
思路
求公差为1的等差数列,在哪些区间内的和为m
对等差数列的前n项和进行化简:
设
可化为:
令,可得:
,
然后对于每一项i判断a和b是不是整数
AC代码
/*
* @Author: WZY
* @School: HPU
* @Date: 2018-10-19 20:21:57
* @Last Modified by: WZY
* @Last Modified time: 2018-10-19 20:39:18
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"---------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int main(int argc, char const *argv[])
{
// ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
double _begin_time = clock();
#endif
int n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
for(int i=sqrt(2*m);i>0;i--)
{
if(2*m%i==0&&(2*m/i-1+i)%2==0)
{
int b=(i-1+2*m/i)/2;
int a=2*m/i-b;
printf("[%d,%d]\n",min(a,b),max(a,b));
}
}
printf("\n");
}
#ifndef ONLINE_JUDGE
double _end_time = clock();
printf("time = %lf ms.", _end_time - _begin_time);
#endif
return 0;
}
HDU 2058:The sum problem(数学)的更多相关文章
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- HDU 2058 The sum problem (数学+暴力)
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
- 题解报告:hdu 2058 The sum problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
- hdu 2058 The sum problem(简单因式分解,,)
Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...
- HDU 2058 The sum problem
传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...
- hdu 2058 The sum problem(数学题)
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
- HDU - 2058 The sum problem(思路题)
题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- HDOJ 2058 The sum problem
Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequen ...
- HDU 5373 The shortest problem (数学)
题意:给定两个数的n和m,有一种操作,把 n 的各位数字加起来放到 n后面形成一个新数n,问重复 m 次所得的数能否整除 11. 析:这个题首先要知道一个规律奇数位的和减去偶数位的和能被11整除的数字 ...
随机推荐
- Notes on Large-scale Video Classification with Convolutional Neural Networks
Use bigger datasets for CNN in hope of better performance. A new data set for sports video classific ...
- js 小说格式整理
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- pyhton 学习 函数式编程
函数是python内建支持的一种封装,我们通过把打断的代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计,函数就是面向过程的程序设计的基本单元 ...
- Yii2.0 数据库查询 [ 2.0 版本 ]
下面介绍一下 Yii2.0 对数据库 查询的一些简单的操作 User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的 ...
- Java四个常用正则表达
1.查询 以下是代码片段: String str="abc efg ABC";String regEx="a|f"; //表示a或fPattern p=P ...
- 十八. Python基础(18)常用模块
十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...
- 去掉HTML标记 .
/// <summary> /// 去除HTML标记 /// </summary> /// <param name=" ...
- Oracle审计策略例子
首先确保Oracle初始化参数audit_trail值为DB或OS,通过“show parameter audit_trail:”查看. 1 语句审计 audit table by test by a ...
- day 32 子进程的开启 及其用法
开启两种子进程的两种方式# # # 1 传统方式# from multiprocessing import Process# import time# def task(name):# print ( ...