HDU 4143 A Simple Problem 题解
题目
For a given positive integer n, please find the saallest positive integer x that we can find an integer y such that \(y^2 = n +x^2\).
输入
The first line is an integer \(T\), which is the the nuaber of cases.
Then T line followed each containing an integer n (\(1 \le n \le 10^9\)).
输出
For each integer n, please print output the x in a single line, if x does not exit , print -1 instead.
样例输入
2
2
3
样例输出
-1
1
题解
\(\because y^2 = n +x^2\)
\(\therefore n=y^2-x^2 = (y+x)(y-x)\)
所以找到两个数字\(a, b\), 满足
\(a \cdot b = n\)
\((a+b)%2==0\) , 因为\((a+b)\%2=(y+x+y-x)\%2=(2\cdot y)\%2=0\)
\(a>b\) , 因为\(x>0\)
\((a-b)\%2==0\), 因为\((a-b)\%2=(y+x-y+x)\%2=(2\cdot x)\%2=0\)
\(a-b=2 \cdot x\)最小, 即\(x\)最小
因为求的是\(a-b\)最小且\(a \cdot b=n\), 所以从\(\sqrt n\) 开始遍历, 若满足条件, 更新最小值, 如果没找到, 就输出\(-1\)
代码
#include <cmath>
#include <cstdio>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, minv = 0x7f7f7f7f, flag = 0;
scanf("%d", &n);
for (int i = 1; i < sqrt(n); i++) {
if (n % i == 0 && (i + n / i) % 2 == 0 && (n / i - i) % 2 == 0 && (n / i - i) > 0) {
flag = 1;
if (n / i - i < minv) minv = n / i - i;
}
}
printf("%d\n", flag ? minv / 2 : -1);
}
return 0;
}
HDU 4143 A Simple Problem 题解的更多相关文章
- 【数论】HDU 4143 A Simple Problem
题目内容 给出一个正整数\(n\),找到最小的正整数\(x\),使之能找到一个整数\(y\),满足\(y^2=n+x^2\). 输入格式 第一行是数据组数\(T\),每组数据有一个整数\(n\). 输 ...
- 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 4267 A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4267 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1016 Prime Ring Problem 题解
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- HDU 2522 A simple problem (模拟)
题目链接 Problem Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第 ...
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
随机推荐
- TZOJ 数据结构实验--静态顺序栈
描述 创建一个顺序栈(静态),栈大小为5.能够完成栈的初始化.入栈.出栈.获取栈顶元素.销毁栈等操作. 顺序栈类型定义如下: typedef struct { int data[Max]; i ...
- 如何快速的找到好玩的旅游景点信息?Python爬虫帮你轻松解决
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 喜欢的朋友欢迎关注小编 当我们出去旅游时,会看这个地方有哪些旅游景点,景点 ...
- SpringCloud多数据源实现
1.枚举多数据源-定义一一对应变量 /** * * 列出所有的数据源key(常用数据库名称来命名) * 注意: * 1)这里数据源与数据库是一对一的 * 2)DatabaseType中的变量名称就是数 ...
- Promise相关学习
what? Promise对象表示一个异步操作的最终状态(完成或失败),以及其返回的值 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/ ...
- 03 . Django之腾讯云短信
简介 由于项目在注册.登录.找回密码 时需要发送短信验证的功能,我们使用腾讯云短信做. 为什么要用腾讯云短信呢? 因为注册就送 100条免费短信 的额度. 注册腾讯云 注册一个腾讯云账户,腾讯云中提供 ...
- 在PyQt5中显示matplotlib绘制的图形
import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import * from plot_pyqt import PlotCanva ...
- 如何修改npm仓库地址
解决方案 npm config set registry http://registry.npm.taobao.org/ 将npm默认设置为淘宝镜像地址 发布包 当你想发布自己的包时,需要将地址修改回 ...
- mybatis的缓存2
原文:https://blog.csdn.net/qq_38274974/article/details/100898145 mybatis的缓存分为一级缓存.二级缓存那么,我们为什么要使用缓存呢? ...
- 通信中的错误代码 (repost from https://blog.csdn.net/zzhuan_1/article/details/80066716)
• 100 - 继续.• 101 - 切换协议.• 110 重新启动标记答复.• 120 服务已就绪,在 nnn 分钟后开始.• 125 数据连接已打开,正在开始传输.• 150 文件状态正常,准备打 ...
- ca78a_c++_字符串流在内存中的输入输出(速度快)
/*ca78a_c++_字符串流在内存中的输入输出**字符串流:在内存中的输入输出.(在内存中进行,速度快)**文件流 :是对文件进行输入和输出.(在磁盘里面进行)istringstream(输入), ...