A Simple Problem
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1650Accepted Submission(s): 424
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
Sample Output
Author
HIT
Source
2011百校联动“菜鸟杯”程序设计公开赛
#include<stdio.h>
#include<math.h>
int main()
{
int i,t,n,x,flag;
scanf("%d",&t);
while(t--)
{
flag=0;
scanf("%d",&n);
i=sqrt(n);
for(i;i>=1;i--)
{
if(n%i==0&&(n/i-i)%2==0&&n/i!=i)
{
x=(n/i-i)/2;
printf("%d\n",x);
flag=1;
break;
}
}
if(flag==0) printf("-1\n");
}
return 0;
}
/*一开时超时 是百分百的事 不过要敢于写出超时的 (在时间还充裕的情况下)
后来一直不知道真么做。 对于这种类型的题目要密切结合式子的特点 把2层循环变成一层
n=(x+y)*(x-y) 让i=x-y n/i做x+y x=(n/i-i)/2;
然后找出各种条件限制 满足则可求出结果
由于程序有可能出现n/i=i的情况 但是题目中题意告诉我们不可能所以千万要排除这种情况
后来在这个地方也错了几次
*/
分享到:
相关推荐
HDU 1022 Train Problem I 附详细思路
1. the number of ways a polygon with n+2 sides can be cut into n triangles 2.the number of ways in which parentheses can be placed in a sequence of numbers to be multiplied, two at a time; 3.the ...
判断某整数是否为2的整数次幂 问题转化为:判断整数是否为正整数且二进制中仅存在一位1 (n > 0 && (n & (n - 1)) == 0) 链表节点交换 修改next指针的值进行节点的交换 修改val字段的值 等价节点交换 练习: leetcode...
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。 现在,给你...
acm hdu as easy as a+b
The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number ...
A simple SDK for HDU. hdu-api 是一个集结 HDU 所有教务管理服务的 SDK,提供了一卡通服务、考试、课表、选课和一些公共信息如空闲教室、上课时间等信息的 API。 hdu-api 主要基于 Requests 库和 Beautiful Soup 库...
2-sat---hdu3062,代码详尽,清晰,格式规范,亲测无误。
本人杭电上题目java实现的代码,绝无其他无用的内容,知道的都晓得这是什么
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
hdu1001解题报告
hdu 1695 GCD(欧拉函数+容斥原理).docx
HDU1059的代码
hdu 1574 passed sorce
考试类精品--A simple SDK for HDU. 一个提供一卡通服务、考试、课表、选课和公共信息等 API
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
int f[m][70]={{1,1},{1,1},{1,2},{1,4}} void add(int p[],int q[],int sum[]) { int i,len if(p[0]<q[0])len=q[0] else len=p[0] for(i=1 i<=len i++) { sum[i]+=q[i]+p[i] if(sum[i]>=...