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 ...
随机推荐
- hrbustoj 1125 循环小数 II(小数变分数+极限思想)
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #i ...
- HDU 3652(数位DP)
题目链接:B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- codeforce 611C New Year and Domino
n*n预处理. 询问的时候用容斥,再删除边界. #include<cstdio> #include<cstring> #include<cmath> #includ ...
- 项目经验分享——Java常用工具类集合 转
http://blog.csdn.net/xyw591238/article/details/51678525 写在前面 本文涉及的工具类部分是自己编写,另一部分是在项目里收集的.工具类涉及数 ...
- astah* professional 6.9.0
下载地址:http://members.change-vision.com/files/astah_professional/6_9_0 破解方法:按照Astah Professional 6.9后打 ...
- CentOS 6.4 x64 Cacti 监控安装配置
Cacti 监控安装配置 环境: 安装Cacti 的服务器 Linux 6.4 x64 ip 10.8.8.11 一: 配置iptables , selinux vi ...
- 如何在Ubuntu中使用Eclipse + CDT开发C/C++程序
在Ubuntu中安装Eclipse和CDT步骤如下: 1. 下载资源(都下载到/home/maxw/Download/Eclipse下) A. 下载JRE(Java Runtime Enviro ...
- FZU 2112 Tickets
这个问题可以转变一下,先要知道有几个连通块,连通块之间肯定需要添加一条边, 还需要知道每个连通块内部需要添加几条边,这个问题等价于求一张图至少需要几笔画成,这个问题的答案是度为奇数的点的个数/2 #i ...
- Listener
通过Listner获得当前的用户个数 package listener; import javax.servlet.ServletContext; import javax.servlet.Servl ...
- Thinking in scala (3)----求平方根
采用“牛顿法”求一个数的平方根 object sqrt { def main(args:Array[String])={ println( sqrt(args(0).toDouble)) } def ...