Pythagorean Triples(Codeforces Round #368 (Div. 2) + 构建直角三角形)

题目链接:

  https://codeforces.com/contest/707/problem/C

题目:

题意:

  告诉你直角三角形的一条边,要你输出另外两条边。

思路:

  我们容易发现除2外的所有素数x作为直角边,那么另外两条边的长度一定为(x * x - 1)/2和(x * x + 1)/2,因此对于每个数我们只需要找到n的最小素因子(除2外)即可,需要额外处理一下2的幂次。

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <deque>
 4 #include <queue>
 5 #include <stack>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <bitset>
 9 #include <cstdio>
10 #include <string>
11 #include <vector>
12 #include <cstdlib>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 typedef long long LL;
19 typedef pair<LL, LL> pLL;
20 typedef pair<LL, int> pLi;
21 typedef pair<int, LL> pil;;
22 typedef pair<int, int> pii;
23 typedef unsigned long long uLL;
24 
25 #define lson rt<<1
26 #define rson rt<<1|1
27 #define lowbit(x) x&(-x)
28 #define name2str(name) (#name)
29 #define bug printf("*********\n")
30 #define debug(x) cout<<#x"=["<<x<<"]" <<endl
31 #define FIN freopen("D://code//in.txt","r",stdin)
32 #define IO ios::sync_with_stdio(false),cin.tie(0)
33 
34 const double eps = 1e-8;
35 const int mod = 1000000007;
36 const int maxn = 2e5 + 7;
37 const double pi = acos(-1);
38 const int inf = 0x3f3f3f3f;
39 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
40 
41 LL t;
42 
43 int main(){
44     scanf("%lld", &t);
45     if(t <= 2) {
46         puts("-1");
47         return 0;
48     }
49     for(int i = 3; i <= sqrt(t); i++) {
50         if(t % i == 0) {
51             LL tmp = t / i;
52             LL x = 1LL * i * i;
53             if(x & 1) {
54                 printf("%lld %lld\n", (1LL * i * i - 1) / 2 * tmp, (1LL * i * i + 1) / 2 * tmp);
55                 return 0;
56             }
57         }
58     }
59     LL num = 1;
60     while(t % 2 == 0) {
61         num = num * 2;
62         t /= 2;
63     }
64     if(t == 1) {
65         t = 4;
66         num /= 4;
67         printf("%lld %lld\n", 3 * num, 5 * num);
68     } else {
69         printf("%lld %lld\n", (t * t - 1) / 2 * num, (t * t + 1) / 2 * num);
70     }
71     return 0;
72 }

猜你喜欢

转载自www.cnblogs.com/Dillonh/p/10452538.html
今日推荐