Codeforces Round #368 (Div. 2) C. Pythagorean Triples
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #368 (Div. 2) C. Pythagorean Triples
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://codeforces.com/contest/707/problem/C
題意:
直角三角形的三邊都為整數,給出其中一邊n,求另外兩邊m、k。
(1?≤?n?≤?109) ?(1?≤?m,?k?≤?1018)
?
分析:
假設直角三角形的斜邊長為c,直角邊為n,a。
則根據勾股定理有a2+n2=c2
變形得到:n2 = (c+a)(c-a)
即因式分解
當n為奇數時,c-a = 1,c+a = n2
當n為偶數時,c-a = 2,c+a = n2/2
聯立可得到c、a的表達式
(分n為奇數和偶數討論,得到n2因式分解時一定有1和2兩個因數,從而構造出可行解)
當n=1和2時,無解要進行特判。
?
代碼:
#include<cstdio> #include<algorithm> #include<map> #include<cstring> #include<string> #include<iostream> #include<set> #include<vector> #include<cmath>using namespace std;typedef long long ll; const int mod = 1000000007; const int maxn = 200010;int main() {ll n,c,a;while(~scanf("%I64d",&n)){if(n<3){printf("-1\n");continue;}if(n&1){c = (n*n+1)/2;a = (n*n-1)/2;}else{c = n*n/4+1;a = n*n/4-1;}printf("%I64d %I64d\n",c,a);}return 0; } View Code?
轉載于:https://www.cnblogs.com/hadis-yuki/p/5792281.html
總結
以上是生活随笔為你收集整理的Codeforces Round #368 (Div. 2) C. Pythagorean Triples的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 月入过万?就靠SEO了
- 下一篇: nexus配置