题目

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\), 满足

  1. \(a \cdot b = n\)

  2. \((a+b)%2==0\) , 因为\((a+b)\%2=(y+x+y-x)\%2=(2\cdot y)\%2=0\)

  3. \(a>b\) , 因为\(x>0\)

  4. \((a-b)\%2==0\), 因为\((a-b)\%2=(y+x-y+x)\%2=(2\cdot x)\%2=0\)

  5. \(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 题解的更多相关文章

  1. 【数论】HDU 4143 A Simple Problem

    题目内容 给出一个正整数\(n\),找到最小的正整数\(x\),使之能找到一个整数\(y\),满足\(y^2=n+x^2\). 输入格式 第一行是数据组数\(T\),每组数据有一个整数\(n\). 输 ...

  2. HDU 4143 A Simple Problem(枚举)

    题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...

  3. 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)/ ...

  4. HDU 4143 A Simple Problem 分解因式

    求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...

  5. HDU 4267 A Simple Problem with Integers

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. HDU 4267 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 1016 Prime Ring Problem 题解

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  8. HDU 2522 A simple problem (模拟)

    题目链接 Problem Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第 ...

  9. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

随机推荐

  1. java实现第六届蓝桥杯立方尾不变

    立方尾不变 立方尾不变 有些数字的立方的末尾正好是该数字本身. 比如:1,4,5,6,9,24,25,- 请你计算一下,在10000以内的数字中(指该数字,并非它立方后的数值),符合这个特征的正整数一 ...

  2. CDN HTTPS安全加速基本概念、解决方案及优化实践

    大家都知道,HTTP 本身是明文传输的,没有经过任何安全处理,网站HTTPS解决方案通过在HTTP协议之上引入证书服务,完美解决网站的安全问题.本文将为大家介绍阿里云CDN HTTPS安全加速传输的基 ...

  3. 循序渐进VUE+Element 前端应用开发(8)--- 树列表组件的使用

    在我前面随笔<循序渐进VUE+Element 前端应用开发(6)--- 常规Element 界面组件的使用>里面曾经介绍过一些常规的界面组件的处理,主要介绍到单文本输入框.多文本框.下拉列 ...

  4. HDFS ha 格式化报错:a shared edits dir must not be specified if HA is not enabled.

    错误内容: Formatting using clusterid: CID-19921335-620f-4e72-a056-899702613a6b2019-01-12 07:28:46,986 IN ...

  5. 【1】svn 指令总结

    [1]svn log 1.svn log 2. [2]svn di [3]

  6. 简述hadoop安装步骤

    简述hadoop安装步骤 安装步骤: 1.安装虚拟机系统,并进行准备工作(可安装- 一个然后克隆) 2.修改各个虚拟机的hostname和host 3.创建用户组和用户 4.配置虚拟机网络,使虚拟机系 ...

  7. test tt=0 <=>test(0)

    class test{ int mvalue; public: test(int i){ cout << "test(int i) =" << i < ...

  8. Java 多线程基础(四)线程安全

    Java 多线程基础(四)线程安全 在多线程环境下,如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线 ...

  9. @atcoder - AGC008E@ Next or Nextnext

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 N 的序列 a,问有多少排列 p,满足对于每一个 ...

  10. redis 的简明教程

    redis 结合ssm使用 一.Redis使用 1.jedis操作redis非关系型数据库 2.spring 集成redis 二.两者区别: 一.Redis使用 1.jedis操作redis非关系型数 ...