HDU 4143 A Simple Problem(数论-水题)

A Simple Problem

Problem Description

For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2.

Input

The first line is an integer T, which is the the number of cases.

Then T line followed each containing an integer n (1<=n <= 10^9).

Output

For each integer n, please print output the x in a single line, if x does not exit , print -1 instead.

Sample Input

2
2
3

Sample Output

-1
1

Author

HIT

Source

2011百校联动“菜鸟杯”程序设计公开赛

Recommend

lcy   |   We have carefully selected several similar problems for you:  4144 4147 4145 4146 4148

题目大意:

给定n,求最小的正整数x,使得 n+x^2也是完全平方数。

解题思路:

假设y^2=n+x^2 ,那么 (y-x)*(y+x)=n,也就是n的两个因子,只需枚举因子即可。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std;

int n;

void solve(){
    int x=(1<<30);
    for(int i=1;i*i<=n;i++){
        if(n%i!=0) continue;
        if( (i&1) == ( (n/i)&1 ) ){
            if((n/i-i)/2>0 && (n/i-i)/2<x ) x=(n/i-i)/2;
        }
    }
    if( x<(1<<30) ) printf("%d\n",x);
    else printf("-1\n");
}

int main(){
    int t;
    scanf("%d",&t);
    while(t-- >0){
        scanf("%d",&n);
        solve();
    }
    return 0;
}

HDU 4143 A Simple Problem(数论-水题)

时间: 2024-08-27 16:40:44

HDU 4143 A Simple Problem(数论-水题)的相关文章

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)/2;  x = (a-b)/2; b越大, x越小, 所以倒着枚举b 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namesp

HDU 4143 A Simple Problem(枚举)

题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了,我也想了一会儿才想起来,真是惭愧啊..... 其实就是将上边那个式子变一下:(y-x)*(y+x) = n ,然后接下来就去枚举(y-x)的值,因为手算了几组数据,发现当这个值越靠近√n时,x的值越小,其实看这个等式也可以看出来,所以枚举的时候从√n这里开始往下枚举就行,然后再看(y-x)+(y+x

hdu 2674 N!Again 数论水题啊~~~

N!Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3803    Accepted Submission(s): 2036 Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                     

HDU 5443 The Water Problem (水题,暴力)

题意:给定 n 个数,然后有 q 个询问,问你每个区间的最大值. 析:数据很小,直接暴力即可,不会超时,也可以用RMQ算法. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue

HDU 5832 A water problem (水题,大数)

题意:给定一个大数,问你取模73 和 137是不是都是0. 析:没什么可说的,先用char 存储下来,再一位一位的算就好了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

HDU 2674 N!Again (数论-水题)

N!Again Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                                     I want to calculate N!...... WhereIsHeroFrom:             So easy! How big N is ? Zty:                                    1 <=

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there