HDU 4143 A Simple Problem

http://acm.hdu.edu.cn/showproblem.php?pid=4143

两个for枚举肯定超时,移项因式分解后,只要一个for枚举(y-x)就好了,(y+x)用n去除(y-x)。

另外两个小点,关注到(y-x)一定小于等于sqrt(n),并且(y-x)与(y+x)不相等

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
int t,n;
int i;
int f;
scanf("%d",&t);
while(t--)
{
f=0;
scanf("%d",&n);
for(i=sqrt(n);i>=1;i--)
{
if(n%i==0&&(n/i-i)%2==0&&n/i!=i)
{
f=1;
break;
}
}
if(f)
printf("%d\n",(n/i-i)/2);
else
printf("-1\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/xiaohongmao/p/2432842.html