您的位置:首页 > 其它

HDU 4143 A Simple Problem

2012-09-12 17:13 393 查看
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)不相等

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
int work(int n)
{
int i,j,k,x,y,f=0;
k=(int)sqrt(n);
for(x=k;x>=1;x--)
{
if(n%x==0&&(x!=n/x)&&(n/x-x)%2==0)
{
f=1;break;
}
}
if(f==0)
return -1;
else
return (n/x-x)/2;
}
int main()
{
int t,i,j,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
m=work(n);
printf("%d\n",m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: