题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间。

析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的。所以我们就想能不能优化一下,找一个范围。想到这是一个连续的序列而且是从1开始的,这不就是一个等差数列么,公差是1罢了。由求和公式得Sn = (a1+an) * n / 2;所以说n最大就是sqrt(M*2)(想一想为什么),因为a1+an 一定是大于n的。如果我们取区间的和,那么Sn = (ai+aj) * (j-i+1)/2;以上我们可得到一个方程,i+j = M/n(当然n|M),j-i+1 = n;所以我们可以解出i和j,其他的就简单了从n到1暴一遍就OK。

当我做完后我又看了网上的题解,我个去,写的比我简单多了。。。

他们是这么说的,等差数列的运用。Sn = (a1+an) * n / 2 = (a1 + a1 + (n - 1) * d)*n/2。

解题公式变形:(a+a+len)*(len+1)/2 = m => a = m/(len+1)-len/2 (m是已知条件,len的最大值为sqrt(2*m))。

代码如下:

这是我写的代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath> using namespace std; int main(){
int n, m;
while(scanf("%d %d", &n, &m)){
if(!n && !m) break;
m <<= 1;
int len = (int)sqrt(m) + 1; while(--len){
if(n > len && m % len == 0){
if((len + m / len - 1) % 2) continue;
int j = (len + m / len - 1) / 2;
if(j > n || j < 0) continue;
int i = m / len - j;
if(i > n || i < 0) continue;
if(i > j) swap(i, j);
printf("[%d,%d]\n", i, j);
}
}
printf("\n");
}
return 0;
}

下面是题解的代码:

#include <cstdio>
#include <cmath>
using namespace std; int main()
{
int n, m, a, len;
while (scanf("%d%d", &n, &m) && (n || m))
{
len = (int)sqrt(2*m);
while (len--)
{
a = m / (len + 1) - len / 2;
if ((2*a+len) * (len+1) / 2 == m)
printf("[%d,%d]\n", a, a+len);
}
printf("\n");
}
return 0;
}

HDU 2058 The sum problem (数学+暴力)的更多相关文章

  1. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  2. 题解报告:hdu 2058 The sum problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...

  3. hdu 2058 The sum problem(简单因式分解,,)

    Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-se ...

  4. HDU 2058 The sum problem

    传送门 Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequen ...

  5. hdu 2058 The sum problem(数学题)

    一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...

  6. HDU - 2058 The sum problem(思路题)

    题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...

  7. HDU 2058 The sum problem 数学题

    解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...

  8. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  9. HDOJ 2058 The sum problem

    Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequen ...

随机推荐

  1. Optimizing graphics performance

    看U3D文档,心得:对于3D场景,使用分层次的距离裁剪,小物件分到一个层,稍远时就被裁掉,大物体分到一个层,距离很远时才裁掉,甚至不载.中物体介于二者之间. 文档如下: Good performanc ...

  2. Beta分布(转)

    背景 在Machine Learning中,有一个很常见的概率分布叫做Beta Distribution: 同时,你可能也见过Dirichelet Distribution: 那么Beta Distr ...

  3. Anaconda中python加入环境变量

    1.我的电脑---高级系统设置 2.选中环境变量,保存. 3.在系统环境变量PATH中,加入Anaconda3及Script路径加入其中 4.测试python

  4. zookeeper集群搭建 windows

    本次zk测试部署版本为3.4.6版本,下载地址http://mirrors.cnnic.cn/apache/zookeeper/ 限于服务器个数有限本次测试了两种情况 1.单节点方式:部署在一台服务器 ...

  5. java 元数据

    什么是元数据? 元数据是指用来描述数据的数据,更通俗一点,就是描述代码间关系,或者代码与其他资源(例如数据库表)之间内在联系的数据.在一些技术框架,如struts.EJB.hibernate就不知不觉 ...

  6. angluarjs ng-repeat 行号

    参考 https://zhidao.baidu.com/question/1882914672116911828.html $index

  7. java学习笔记----@Override的作用

    初学java或多或少都会有这样的疑问,@Override有什么用,有的时候写,有的时候又不写,搞的初学者甚是郁闷. 做了一两年的开发说起这个问题不一定能够对答如流.小弟才疏学浅,花了点时间,看了一下资 ...

  8. vs2015安装出问题

    win7系统需要更新serverpage1包,更新完就ok了,ie不用升级到ie10

  9. TOYS(叉积)

    TOYS http://poj.org/problem?id=2318 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 193 ...

  10. oracle在centos6.5安装

    说明 很多操作是默认,具体定制另说. 安装 参考http://www.linuxidc.com/Linux/2014-02/97374p4.htm 这篇是上面那篇的整合版:http://www.cnb ...