HDU 2058 The sum problem (数学+暴力)
题意:给定一个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 (数学+暴力)的更多相关文章
- 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
题目链接: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;很显然,这是一个 ...
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...
- HDOJ 2058 The sum problem
Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequen ...
随机推荐
- Simple2D-26 Simple2D 最后的工作,开发结束
开始的时候打算将 Simple2D 做成一个库的,但现在没有那个功夫了. 要渲染顶点数据,就必须将渲染函数放置到 glClear( ) 函数和 SwapBuffers( ) 函数之间,但又不希望开发时 ...
- LaiFeng-code
https://github.com/LaiFeng-Android/SopCastComponent https://github.com/LaiFengiOS/
- python 升级到python2.7
查看python的版本 [root@localhost ~] python -V Python 2.4.3 1.先安装GCC yum -y install gcc 如果安装gcc 出错, yum ...
- DELL服务器iDRAC相关设置
iDRAC又称为Integrated Dell Remote Access Controller,也就是集成戴尔远程控制卡 iDRAC卡相当于是附加在服务器上的一台小电脑,通过与服务器主板上的管理芯片 ...
- 迷你MVVM框架 avalonjs 学习教程21、双向绑定链
avalon的双向绑定机制,是通过一条依赖链实现.此依赖链最底层是监控属性.监控数组,中层是计算属性.监控函数,再上点是求值函数,最上层是视图刷新函数. 所谓计算属性,监控属性,监控函数属性,我们改变 ...
- scala 建模
// train multinomial logistic regression val lr = new LogisticRegressionWithLBFGS() .setIntercept(tr ...
- break、continue、pass介绍
break.continue.pass介绍 break:跳出当前循环 continue:跳出本次循环,进行下一次循环 pass:什么也不做,占位.
- uboot的配置及编译
1. 先执行配置命令 make board_name_config 再执行编译命令 make all 2. 通过在Makefile中找到 board_name_config 目标,可以查看为了得到目标 ...
- 前后端分离 开发环境通过CORS实现跨域联调
通过JSONP实现跨域已是老生常谈,JSONP跨域限制多,最近了解了一下CORS. 参考: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Acce ...
- ensembl数据库的使用方法
1)下载各种数据bam.gtf.fasta.ded等的地址 ftp://ftp.ensembl.org/../pub/release-93/