Hdu 4143
好久没有在Hdu水题了,于是乎在无聊之际还是找了一道水题,
但是看完题目之后,明显是个数学题,我还是感觉有点打触的。
因为一直对数学题没有多大信心。
分析了一下,Y^2 = X^2 + n 可以转化为 Y^2 = (X + a)^2
所以:n = a^2 + 2 * a * X , 而且 X > 0
所以 0 < a <= sqrt(n + 1) - 1
由于要求的是最小的 X, 所以只要逆序枚举 a 就可以了,
这里复杂度为 O(sqrt(n)), 所以可解。
附上代码:
#include <cmath>
#include <cstdio> typedef long long LL;
#define min(x, y) ((x) < (y) ? (x) : (y)) int main() {
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n); int ans = -; int k = int(sqrt(n + 1.0)) - ;
for (int i = k; i > ; i--) {
long long tmp = n - (long long)i * i;
if (tmp % ( * i))
continue;
else {
if (ans == -)
ans = tmp / ( * i);
else
ans = min(ans, tmp / ( * i));
break;
}
}
printf("%d\n", ans);
} return ;
}
Hdu 4143的更多相关文章
- HDU 4143 A Simple Problem(枚举)
题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...
- hdu 4143 A Simple Problem (变形)
题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n), y = (a+b)/ ...
- HDU 4143 A Simple Problem 分解因式
求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...
- HDU 4143 A Simple Problem 题解
题目 For a given positive integer n, please find the saallest positive integer x that we can find an i ...
- 【数论】HDU 4143 A Simple Problem
题目内容 给出一个正整数\(n\),找到最小的正整数\(x\),使之能找到一个整数\(y\),满足\(y^2=n+x^2\). 输入格式 第一行是数据组数\(T\),每组数据有一个整数\(n\). 输 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- Ubuntu时间管理方法
1. date 命令主要用于显示以及修改系统时间 2. hwclock 命令用于查看设置硬件时间,以及同步硬件时间与系统时间 # 显示硬件时间hwclock # 设置硬件时间hwclock -set ...
- Linux产生coredump文件(core)
1.可以使用命令 ulimit -c unlimited 来开启 core dump 功能,并且不限制 core dump 文件的大小: 如果需要限制文件的大小,将 unlimited 改成你想生成 ...
- PAT甲级——A1065 A+B and C (64bit)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...
- PAT甲级——A1036 Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- java 遍历
LinkedList倒序遍历 public List<Integer> getNewsFeed(int userId) { List<Integer> res = new Ar ...
- iBaties对比hibernate
翻译至一篇2008年的文章(http://www.javaworld.com/article/2077875/open-source-tools/ibatis--hibernate--and-jpa- ...
- Lowest Common Ancestor (LCA)
题目链接 In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defi ...
- redis教程(二)-----redis事务、记录日志到redis、分布式锁
redis事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 批量操作在发送 EXEC 命令前被放入队列缓存. 收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余 ...
- cookie-在关闭浏览器之前弹框只弹一次
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- java list转换json格式
/** * 处理返回值(转换json格式和补零) * * @param resultDto5List * @param dateList * @return */ private JSONObject ...