类型:数学
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4206
来源:BAPC 2011
思路:y^2 - x^2 = n =》 (y - x) * (y + x) = n 。容易知道,y - x 与 y + x均为正整数【4206为非负整数】,且y - x <= sqrt(n)
枚举i = y - x, 那么y + x = n / i,有x = (n / i - i) / 2 > 0, y = (n / i + i) / 2
x 随i增长单调减,i从大到小枚举
6206
// hdoj 4206 Treasure Map
// ac 15MS 232K
#include<cstdio>
#include<cmath>
using namespace std;
int main() {
int s, n, i;
scanf("%d",&s);
while(s--) {
scanf("%d", &n);
for(i = sqrt((double)n); i >= 1; --i)
if(n % i == 0 && (n / i - i) % 2 == 0) {
printf("%d %d\n", (n / i - i) / 2, (n / i + i) / 2);
break;
}
if(i == 0)
printf("IMPOSSIBLE\n");
}
return 0;
}
4143
// hdoj 4143 A Simple Problem
// ce ac 31MS 232K
#include<cstdio>
#include<cmath>
using namespace std;
int main() {
int s, n, i;
scanf("%d",&s);
while(s--) {
scanf("%d", &n);
for(i = sqrt((double)n); i >= 1; --i)
if(n % i == 0 && (n / i - i) % 2 == 0 && (n / i - i > 0)) {
printf("%d\n", (n / i - i) / 2);
break;
}
if(i == 0)
printf("-1\n");
}
return 0;
}