HDU-2058-The sum problem(数学题技巧型)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2058
思路:
这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式,
(1)sn=n*(a1+an)/2; 即可代入公式,(2)m=(e-s+1)*(s+e)/2 注释*******//s代表起点,e代表终点。
则由(2)推出,(3)e=(int)(2*m+s*s-s),根据e点找s点,代入(1)成立则输出[s,e]; 但新的
问题又出现了由于m可能很大所以(3)中e可能太大而溢出。也不行,
同样的思路,可以换一种写法,(4)m=j*(i+i+j-1)/2 注释*****//i代表起点,j代表数的个数;
那么 j=sqrt(2*m); j>=1;j-- 一直递减找下去,由j算出i,代入i,j 如果 (4)成立
则输出【i,i+j-1】;
我的AC代码
#include<stdio.h>
#include<math.h>
int main(void)
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)==2&&(n+m))
{
for(j=sqrt(2*m);j>0;j--)
{
i=(2*m/j-j+1)/2;
if(i+j-1<=n) //这个题,数据很不全,这一行如果不写同样可过,但是错的, 如 n< m 错误就体现出来了。
if(j*(2*i+j-1)==2*m)
{
printf("[%d,%d]\n",i,i+j-1);
}
}
printf("\n");
}
return 0;
}
HDU-2058-The sum problem(数学题技巧型)的更多相关文章
- HDU 2058 The sum problem 数学题
解题报告:可以说是一个纯数学题,要用到二元一次和二元二次解方程,我们假设[a,b]这个区间的所有的数的和是N,由此,我们可以得到以下公式: (b-a+1)*(a+b) / 2 = N;很显然,这是一个 ...
- 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(数学题)
一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...
- 题解报告: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(思路题)
题目: Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the ...
- HDU 2058 The sum problem (数学+暴力)
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
- hdu 4961 Boring Sum(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...
随机推荐
- 使用GDB调试Android NDK native(C/C++)程序-转
最 近写了些需要跨平台兼容的C++库,Android是其中需要兼容的平台之一.区别于Windows,Mac中功能强大的IDE环境,官方并没有为 Android ndk提供太多的支持.因此,尝试了下通过 ...
- PAT (Advanced Level) 1063. Set Similarity (25)
读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...
- HDU 5523 Game
坑题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...
- Python3基础 list(zip()) 将两个列表打包起来
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- ecos的model
表->dbschema->model 虚拟化model机制 在dbschema存在model不存在的情况下 很多mvc结构都这么来 model命名规则 {$app_name}_mdl_{$ ...
- ural1470 UFOs
UFOs Time limit: 2.0 secondMemory limit: 64 MB Vasya is a ufologist and his duties include observing ...
- CodeForces 610B Vika and Squares
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- mac 剪切文件
首先选中文件,按Command+C复制文件:然后按Command+Option+V:就可以把你的文件剪走了!在这里补充一下,我这里讲的是剪切文件夹,不是剪切文本和文字!Command+X只能剪切文字文 ...
- IBATIS事务处理 - - 博客频道 - CSDN.NET
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- mysql 数据库 切表的脚本
#!/bin/sh host=$1 port=$2 host=${host:="localhost"} #host没赋值,那么就赋值为localhost port=${port: ...