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 ...
随机推荐
- apk文件分析原则
如果在dex生成的jar文件里没有发现关键内容的话,就要注意jar里面的native函数以及loadlibrary操作,从而可以判断出加载了哪些so,调用了什么函数.就不会出现判断不出是不是加载了某s ...
- Chapter 1 First Sight——6
"You didn't need to do that, Dad. I was going to buy myself a car." 你不需要这样,父亲,我会自己买一辆车的 &q ...
- 关于struts2的web.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- C#+QI的例子
COM中,和我们打交道的是接口,也就是说类对我们是隐形的,那么我们要做开发,要使用这些功能,我们只能通过接口,通过接口暴露出来的方法,COM是一种服务器端/客户端架构,服务器端定义了操作的法,客户端通 ...
- PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)
树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- offsetTop、clientTop、scrollTop、offsetTop各属性介绍
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- [算法] kruskal最小生成树算法
#include <stdio.h> #include <stdlib.h> #define MAX 100 int N, M; struct Edge { int u,v; ...
- 完美解决ie浏览器location.href不刷新页面的问题,进入页面只刷新一次
/* ie不刷新列表bug */try{ var agent = navigator.userAgent.toLowerCase(); var ieflag = /(msie\s|trident.*r ...
- Charles从入门到精通
Charles 从入门到精通 发表于 2015-11-14 12:00 文章目录 1. 目录 2. 简介 3. 安装 Charles 4. 将 Charles 设置成系统代理 5. Charles 主 ...
- iOS校验身份证是否合法
//身份证号验证 1900+/2000+的年份日期的正则表达式经过修改,目前貌似是对的,如果哪位朋友发现错误希望能够给与提示 //返回yes位表示格式正确,否则为错误 -(BOOL)IDCardAut ...