hdoj:2058
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; struct node{
int left;
int right;
};
int main()
{
long N, M;
long long sum = ;
vector<node> nodes;
while (cin >> N >> M)
{
if (N == && M == )
break; for (long n = N; n >=; n--)
{
if ((*M-n*n+n)%(*n)!=)
continue;
long long a1 = ( * M - n*n + n) / ( * n); long long an = a1 + n - ;
if (a1 >= && a1 <= N && an >= && an <= N && a1 <= an)
{
//node node;
//node.left = a1;
//node.right = an;
//nodes.push_back(node);
cout << "[" << a1 << "," << an << "]" << endl;
}
}
//for (node node : nodes)
//{
// cout << "[" << node.left << "," << node.right << "]" << endl;
//}
cout << endl;
}
return ;
}
超时
a1 = k
an = k + n - 1
M = n*(2k+n-1)/2
解的
k = M/n - (n-1)/2,这个不能这样写,要写在一起
(2M-n*n-n)/(2*n)。否则对于30/4 - 3/2,会忽略这样的结果,如果先通分当然也就可以。
超时
利用a1>=1这个条件可以减少循环次数
2M - n*n >n
2M > n*n - n>n*n
sqrt(2M)>n
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; struct node{
int left;
int right;
};
int main()
{
long N, M;
long long sum = ;
vector<node> nodes;
while (cin >> N >> M)
{
if (N == && M == )
break; for (long n = sqrt(*M); n >=; n--)
{
if ((*M-n*n+n)%(*n)!=)
continue;
long a1 = ( * M - n*n + n) / ( * n); long an = a1 + n - ;
if (a1 >= && a1 <= N && an >= && an <= N && a1 <= an)
{
//node node;
//node.left = a1;
//node.right = an;
//nodes.push_back(node);
cout << "[" << a1 << "," << an << "]" << endl;
}
}
//for (node node : nodes)
//{
// cout << "[" << node.left << "," << node.right << "]" << endl;
//}
cout << endl;
}
return ;
}
hdoj:2058的更多相关文章
- 算法——A*——HDOJ:1813
Escape from Tetris Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdoj:题目分类
基础题: 1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058. ...
- HDOJ:1533-Going Home(最小费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 解题心得: 第一次写最小费用流的题,去hdoj上找了一个入门级题目,建图比较简单,用了spfa和 ...
- hdoj:2086
A1 = ? Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdoj:2085
核反应堆 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdoj:2084
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...
- HDOJ:6356-Glad You Came(线段树剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356 解题心得: 现在深深的知道了算法复杂度的重要了,这个题算复杂度的时候还要把一些常数也算出来,不然 ...
- hdoj:2075
A|B? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- Linux之经典互联网架构
经典互联网架构 netstat -tulnp |grep 80ss -tulnp|grep 80 前期铺垫: 1. Linux要能上网2. 掌握Linux软件包安装方法2.1 rpm包管理 2.1.1 ...
- YIi url美化
一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...
- Incorrect username or password ( access token )解决
Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...
- securecrt通过ssh连接板子: 密钥交换失败,没有兼容的加密程序
在用securecrt连接板子时遇到如下问题: 需要修改板子上的/etc/ssh/ssh_config和/etc/ssh/sshd_config. 修改/etc/ssh/ssh_config,取消下面 ...
- 修正mui的上拉加载和下拉刷新的一次未完成就执行下一次的请求
有的用户会在第一次 上拉和下拉 事件未完成的时候,就执行第二次,第三次请求.... 提示文字也加上的位置动态计算,修改了mui的部份源码实现,我们的需求达到了 我们来看一下 腾讯新闻 的效果:
- Linux(CentOS)下,下载安装Nginx并配置
1.准备工作选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL.Nginx是C写的,需要用GCC编译:Nginx的 ...
- spring注解之@PostConstruct在项目启动时执行指定方法
一.注解解释 Spring的@PostConstruct注解在方法上,表示此方法是在Spring实例化该Bean之后马上执行此方法,之后才会去实例化其他Bean,并且一个Bean中@PostConst ...
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- Python数据预处理:机器学习、人工智能通用技术(1)
Python数据预处理:机器学习.人工智能通用技术 白宁超 2018年12月24日17:28:26 摘要:大数据技术与我们日常生活越来越紧密,要做大数据,首要解决数据问题.原始数据存在大量不完整.不 ...
- Oracle 学习笔记 10 -- 约束
本次笔记来学习约束,约束在表中无处不在. 比如,表中的数据不同意为空或者是表中id为设为主键,都是约束. 约束分类: 主键约束(PRIMARY KEY):主键表示表中一个唯一的标识,本 ...