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整除的数字 ...
随机推荐
- 随机数类Random
我们来学习下,用来产生随机数的类Random,它也属于引用数据类型. 这个Random类,它可以产生多种数据类型的随机数,在这里我们主要介绍生成整数与小数的方式. l 方法简介 public int ...
- java基础语法运算符
1.1 算数运算符++.--的使用 在一般情况下,算数运算符不会改变参与计算的变量值.而是在原有变量值不变的情况下,计算出新的值.但是有些操作符会改变参与计算的变量的值, ...
- JQuery的选择器的简单介绍
1.jquery工厂函数 介绍Jquery选择器前,先来说一下JQuery的工厂函数"$",在JQuery中,无论使用哪种类型选择符都要从一个“$”符号和一对“()”开始. 在“( ...
- excle
1.固定某行列 如果要使一行不动,将光标定位于A2单击中,单击菜单"窗口----冻结窗格" 一行一列的,光标定位于B2单元格中,其它的以此类推 2.自动排序号 自动排序号,就是在某 ...
- 【JAVA】关于向上转型与向下转型
向上转型: 子类引用的对象转换为父类类型称为向上转型.通俗地说就是是将子类对象转为父类对象.此处父类对象可以是接口 如果子类重写了父类的方法,就根据这个引用指向调用子类重写的这个方法,不是调用父类的, ...
- day27-python阶段性复习-基础
一.基础资料,安装python Python 跨平台的,(Linux,Windows,mac) 网站www.python.org 解释器交互方式 Ipython Python shell https: ...
- 1.DNS基础及域名系统架构
一.域名: IP地址往往难以记忆,所以我们一般使用域名进行管理 www.LinuxCast.net 1.严格的域名最后还有一个".",但一般省略不写 2.域名分为三个部分,用&qu ...
- JavaScript -基础- 变量、常量
一.变量 <script> var a=1 var b=3 var a= 1; //使用var 定义变量,分号结尾(可不加,换行符也可) var b=3; var a= 1; var ...
- zTree入门实例(一眼就看会)
zTree 是一个依靠 jQuery 实现的多功能 “树插件”. 下载地址:https://gitee.com/zTree/zTree_v3 待会将上面划的三个文件复制到Java的Web工程下即可 先 ...
- c# async/await异步编程死锁的问题
在异步编程中,如果稍有不注意,就会造成死锁问题.何为死锁:即两个以上的线程同时争夺被互相锁住的资源,两个都不放手. 在UI或asp.net中,容易造成死锁的代码如下所示: private void b ...