codeforces 707C - Pythagorean Triples
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.
For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.
Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.
Katya had no problems with completing this task. Will you do the same?
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.
Print two integers m and k (1 ≤ m, k ≤ 1018), such that n, m and k form a Pythagorean triple, in the only line.
In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.
3
4 5
6
8 10
1
-1
17
144 145
67
2244 2245
题意:给你一个数,问能否找出另外任意两个数和这个数组成勾股数,
分析:..首先想到的就是之前看的一个勾股数组定理的结论(证明当时没记 (*^▽^*)!!!);
k = (s * s + t * t) / 2 * ans;
1 #include <bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 const int maxn = 1e6+5; 5 6 signed main() 7 { 8 int n, m, k; 9 while(~scanf("%lld", &n)) 10 { 11 if(n == 1) 12 { 13 printf("-1\n"); 14 continue; 15 } 16 int s, t; 17 int ans = 1; 18 int tep = 2048; 19 while(tep != 1) 20 { 21 while(n % tep == 0) 22 { 23 n /= tep; 24 ans *= tep; 25 } 26 tep /= 2; 27 } 28 if(n == 1) 29 { 30 n = 4; 31 ans /= 4; 32 m = 3 * ans; 33 k = 5 * ans; 34 } 35 else if(n % 2) 36 { 37 s = n; 38 t = 1; 39 m = (s * s - t * t) / 2 * ans; 40 k = (s * s + t * t) / 2 * ans; 41 } 42 if(!m || !k) 43 { 44 printf("-1\n"); 45 } 46 else 47 printf("%lld %lld\n", m, k); 48 } 49 return 0; 50 } 51 52 /* 53 54 55 */