HDU 4206 Treasure Map

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4206

大意:给你一个数n,问b^2-a^2 == n,求出满足条件的a 和b,若有多个输出最小的那个,若没,输出IMPOSSIBLE

 

b^2-a^2 = (b-a)*(b+a) = p * q = n

因为1<=n<=10^9,现在我们只要枚举p从1到根号10^9,

a = (q-p)/2;
b = (q+p)/2;其中(q-p)和(q+p)都为偶数,n%p == 0(用于剪枝)

然后枚举的时候取最小的a

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

const int INF = 1<<30;
int main()
{
int t;
int n;
int i, j;
int p, q;
int a, b;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
a = INF;
for (p = 1; p <= (int)sqrt(double(n)); p++)
{
if (n % p == 0 && (n/p - p) % 2 == 0)
{
q = n/p;
if (a > (q-p)/2)
{
a = (q-p)/2;
b = (q+p)/2;
}
}
}
if (a == INF)
{
puts("IMPOSSIBLE");
}
else
{
printf("%d %d\n", a, b);
}
}
return 0;
}

 

 

转载于:https://www.cnblogs.com/qiufeihai/archive/2012/04/04/2431899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值