题意:给定一个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. Maven打包跳过测试,文档生成

    运行mvn install时跳过Test 方法一: <project> [...] <build> <plugins> <plugin> <gro ...

  2. 通过http方式关闭和重启Jenkins

    Jenkins关闭和重启实现方式.   1.关闭Jenkins 只需要在访问jenkins服务器的网址url地址后加上exit.例如我jenkins的地址http://localhost:8080/, ...

  3. Jstl标签<c:forEach>的用法

    <c:forEach>除了支持数组之外,还有标准J2SE的集合类型,例如:ArrayList.List.LinkedList.Vector.Stack和Set 等等:另外还包括java.u ...

  4. autoface

    Autofac 依赖注入框架 使用 2015-08-02 10:20 by jiangys, 36262 阅读, 4 评论, 收藏, 编辑 简介 Autofac是一款IOC框架,比较于其他的IOC框架 ...

  5. jQuery中的几个模块总结

    Query插件,以备并希望在前端方面有所长进.请批评指正. 一,类型判断全解 JQuery判断类型扩展方法:$.type() /*type: function( obj ) { if ( obj == ...

  6. 《Blue Flke》第一次作业:团队亮相

    1.队名:Blue Flke 团队格言:决心是成功的力量,耐心是成功的保障. 2.团队成员组成:  201571030129/ 王胜海 (组长)  201571030126/ 妥志福 20157103 ...

  7. Appium客户端,命令行启动server

    目标:通过命令行启动Appium的server   1.通过命令行安装的Appium   直接命令行输入appium即可启动服务   2.安装的Appium客户端   可以查看客户端中打印的启动日志: ...

  8. 第六章 图(b1)邻接矩阵

  9. SVN版本冲突问题

    --------------------siwuxie095 SVN 版本冲突问题 如:Jack 和 Mary 从仓库中将项目下载到本地,然后 Jack 修改了 项目中的一个文件,并上传到仓库中,之后 ...

  10. REVERSE!REVERSE!REVERSE!

    形式汇总: 206. Reverse Linked List 92. Reverse Linked List II:Given a string and an integer k, you need ...