Codeforces Round #368 (Div. 2) C. Pythagorean Triples
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #368 (Div. 2) C. Pythagorean Triples
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:http://codeforces.com/contest/707/problem/C
題意:
直角三角形的三邊都為整數(shù),給出其中一邊n,求另外兩邊m、k。
(1?≤?n?≤?109) ?(1?≤?m,?k?≤?1018)
?
分析:
假設(shè)直角三角形的斜邊長為c,直角邊為n,a。
則根據(jù)勾股定理有a2+n2=c2
變形得到:n2 = (c+a)(c-a)
即因式分解
當(dāng)n為奇數(shù)時(shí),c-a = 1,c+a = n2
當(dāng)n為偶數(shù)時(shí),c-a = 2,c+a = n2/2
聯(lián)立可得到c、a的表達(dá)式
(分n為奇數(shù)和偶數(shù)討論,得到n2因式分解時(shí)一定有1和2兩個(gè)因數(shù),從而構(gòu)造出可行解)
當(dāng)n=1和2時(shí),無解要進(jìn)行特判。
?
代碼:
#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?
轉(zhuǎn)載于:https://www.cnblogs.com/hadis-yuki/p/5792281.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #368 (Div. 2) C. Pythagorean Triples的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 月入过万?就靠SEO了
- 下一篇: JavaScript Ajax