题目

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. HDU-4341 Gold miner 题解

    题目大意 黄金矿工的游戏,不过每个金块可以看做是质点,没有大小,给出每个金块的坐标.抓取所花费的时间(包括返回的时间),以及价值,其中有一些金块可能会共线.求在规定时间内所获得的最大价值. 样例 样例 ...

  2. Go语言圣经[中文版]

    近期整理了一篇Go语言圣经[中文版]在线版本,排版比较适合手机以及PC阅读. Go语言圣经[中文版本]

  3. Dedecms中{dede:type}标签支持调用父级栏目名称

    需求: 我们用{dede:type}标签调用栏目相关内容时,同时需要调用该栏目的父级栏目的名称. {dede:type}标签的代码做了一下开发,支持这个调用了. 开发方法: 1.打开include/t ...

  4. ORM框架 Mybatis、Hibernate、Spring Data JPA之到底该用谁,谁更牛*

    在持久层框架中无可厚非的就是mybatis了,但是也会经常被人问到为啥要用mybatis,为啥不用hibernate,jpa.很多人各级都是地铁爷爷看手机的表情,似乎从来没想过这个问题.“公司叫用我就 ...

  5. mysql关于group by的用法

    原文:https://blog.csdn.net/u014717572/article/details/80687042 先来看下表1,表名为test: 表1 执行如下SQL语句: SELECT na ...

  6. Activity学习笔记1

    Activity概述 简单的理解Activity就是指Android手机或平板的一个屏,类似Window的一个窗口,浏览器的一个页面. Activity的4种状态 Activity的生命周期 创建Ac ...

  7. deepin文件用途

    Bin:二进制文件, 存放二进制文件Dev:存放外接设备,其中外接设备不能被直接使用需要挂载(启动设备)Etc:存放配置文件Home:家目录,出了root用户外的其他用户类似于Windows中的use ...

  8. ida 调试android之路

    系统: Mac OSX 调试环境:IDA7.0,  adb 手机环境:红米手机 android 4.4.4 前提条件: 红米手机root之路:https://www.cnblogs.com/dzqdz ...

  9. break,continue,break的用法与区别

    1.return 语句的作用 (1) return 从当前的方法中退出,返回到该调用的方法的语句处,继续执行.       (2) return 返回一个值给调用该方法的语句,返回值的数据类型必须与方 ...

  10. 定时任务Cron

    Linux系统中的定时任务cron,一个很实际很有效很简单的一个工作,在日常的生产环境中,会被广泛使用的一个组件.通过设置时间.执行的脚本等内容,能够让系统自动的执行相关任务,很是方便. cron定时 ...