『算法-ACM竞赛-数学-数论』Find Integer(勾股数定理)

『算法-ACM 竞赛-数学-数论』Find Integer(勾股数定理)

Problem Description

1
2
3
4
5
people in USSS love math very much, and there is a famous math problem
give you two integers n,a,you are required to find 2 integers b,c such that an+bn=cn.
Input
one line contains one integer T;(1≤T≤1000000)
next T lines contains two integers n,a;(0≤n≤1000,000,000,3≤a≤40000)

Output

1
2
print two integers b,c if b,c exits;(1≤b,c≤1000,000,000)
else print two integers -1 -1 instead.

Sample Input

1
2
1
2 3

Sample Output

1
4 5

Source

1
2018中国大学生程序设计竞赛 - 网络选拔赛

勾股数定理详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll a, n;
scanf("%lld%lld", &n, &a);
if (n > 2 || n == 0)
printf("-1 -1\n");
else
{
ll b, c, s;
if (n == 1)
printf("1 %lld\n", a + 1);
else if (n == 2)
{
if (a % 2 == 1)
{
b = a * a / 2;
c = b + 1;
}
else
{
s = a * a / 2;
b = s / 2 - 1;
c = s / 2 + 1;
}
printf("%lld %lld\n", b, c);
}
}
}
return 0;
}

『算法-ACM竞赛-数学-数论』Find Integer(勾股数定理)
https://chiamzhang.github.io/2024/06/29/『算法-ACM竞赛-数学-数论』Find Integer(勾股数定理)/
Author
Chiam
Posted on
June 29, 2024
Licensed under